试试这样的
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;
}
}