回显购物车中的产品ID和产品项目密钥

时间:2018-07-30 作者:Jyoti Sharma

如何回显每个购物车项目的产品id和product\\u item\\u密钥,而不是总计数?

function iconic_cart_count_fragments( $fragments ) {

    $fragments[\'div.header-cart-count\'] = \'<div class="header-cart-count">\' . WC()->cart->get_cart_contents_count() . \'</div>\';

    return $fragments;

} 
谢谢你

2 个回复
最合适的回答,由SO网友:nmr 整理而成

技术设计者,可能是错误的foreach 循环设置值为$fragments[\'div.header-cart-count\'] 而是附加它。

尝试此修改:

function iconic_cart_count_fragments( $fragments ) {

     foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { 
        $fragments[\'div.header-cart-count\'] .= \'<div class="header-cart-count">\' .$cart_item_key.\'<br><br>\'. $cart_item[\'product_id\']. \'</div>\'; 
     } 
     return $fragments; 
} 
或:

function iconic_cart_count_fragments( $fragments ) {

     $cart = WC()->cart->get_cart();
     if (!empty($cart)) {
         foreach ( $cart as $cart_item_key => $cart_item )
            $output .= $cart_item_key. \' - \' . $cart_item[\'product_id\'] . \'<br>\';
         $fragments[\'div.header-cart-count\'] = \'<div class="header-cart-count">\' . $output . \'</div>\'; 
     }
     return $fragments; 
} 

SO网友:Techno Deviser

试试这样的

global $woocommerce;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {

echo $cart_item_key;
echo $cart_item[\'product_id\'];
}

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
echo $cart_item_key;
echo $cart_item[\'product_id\'];
}

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
 $fragments[\'div.header-cart-count\'] = \'<div class="header-cart-count">\' .$cart_item[\'product_id\']. \'</div>\'; 
 }
这将为您提供产品ID,而不是当前输出。

最终工作解决方案。

 global $woocommerce; 
 add_filter( \'woocommerce_add_to_cart_fragments\', \'iconic_cart_count_fragments\', 10, 1 ); 
 function iconic_cart_count_fragments( $fragments ) {
     foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { 
     $fragments[\'div.header-cart-count\'] = \'<div class="header-cart-count">\' .$cart_item_key.\'<br><br>\'. $cart_item[\'product_id\']. \'</div>\'; 
     return $fragments; 
     } 

     } 

结束

相关推荐