簡體   English   中英

在WooCommerce中,購物車商品標題未連接到HTML表中

[英]Cart item title isn't concatenated into an HTML table in 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表格的側面打印的項目名稱以黃色突出顯示。

在此處輸入圖片說明

我的問題是

  1. 這是什么原因呢?
  2. 如何解決這個問題?

TIA。

嘗試下面的代碼,可能是串聯的跳過代碼

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;
}

您只是忘了在標題周圍添加<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;
}

代碼在您的活動子主題(或主題)的function.php文件中,或者在任何插件文件中。

經過測試,現在可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM