车项目标题不将连接到一个HTML表中WooCommerce

问题描述:

我已创建一个短代码列出所有的项目名称如下,车项目标题不将连接到一个HTML表中WooCommerce

add_shortcode('show_cart_items', 'tcf_show_cart_items'); 
function tcf_show_cart_items() 
{ 
    $cart = '<table>'; 
       foreach( WC()->cart->get_cart() as $cart_item) 
       { 
        $cart .= '<tr>' . $cart_item['data']->get_title() . '</tr>'; 
       } 
    $cart .= '</table>'; 

    return $cart; 
} 

,这是工作正常,但我现在面临的是打印出表格的项目。当我在网页上检查时,您可以将输出看作截图,而在HTML表格中未打印的项目名称以黄色突出显示。

enter image description here

我的问题是,

  1. ,这是什么原因呢?
  2. 如何解决这个问题?

TIA。

+0

这听起来像'get_title'方法实际上是输出标题(回声,打印),而不是返回它......但根据文档,它应该做后者(至少在WC 3,你是哪个版本使用?) – CBroe

+0

我使用WC 3,最新的。 – mapmalith

尝试下面的代码,可能是连击跳跃代码

add_shortcode('show_cart_items', 'tcf_show_cart_items'); 
function tcf_show_cart_items() 
{ 
    $cart_item = ''; 

    foreach( WC()->cart->get_cart() as $cart_item) 
    { 
     $cart_item .= '<tr>' . $cart_item['data']->get_title() . '</tr>'; 
    } 

    $cart = '<table>'.$cart_item.'</table>'; 

    return $cart; 
} 
+0

它给出了相同的输出,没有运气 – mapmalith

你刚才忘了添加标题周围<td> HTML标签是这样的:

add_shortcode('show_cart_items', 'tcf_show_cart_items'); 
function tcf_show_cart_items() 
{ 
    $cart = '<table>'; 
       foreach( WC()->cart->get_cart() as $cart_item) 
        $cart .= '<tr><td>' . $cart_item['data']->get_title() . '</td></tr>'; 
    $cart .= '</table>'; 

    return $cart; 
} 

也或者是这样的:

add_shortcode('show_cart_items', 'tcf_show_cart_items'); 
function tcf_show_cart_items() 
{ 
    $cart = '<table><tr>'; 
       foreach( WC()->cart->get_cart() as $cart_item) 
        $cart .= '<td>' . $cart_item['data']->get_title() . '</td>'; 
    $cart .= '</tr></table>'; 

    return $cart; 
} 

Code goe s在你的活动子主题(或主题)的function.php文件中,或者也在任何插件文件中。

经过测试,现在就开始工作。