我使用ide主题。我需要改变总购物车项目计数显示在标题菜单迷你推车图标。我有两种产品。一个是基于体重的,另一个是正常的。例:我的购物车项目=>产品1 - 35克- $50;产品2 - 2(项目)- $30.00;这里我需要总购物车项目计数为3。但现在显示为37。
这是我的代码。
<?php
add_filter( 'woocommerce_add_to_cart_fragments', 'add_to_cart_fragment',10,1);
function add_to_cart_fragment($fragments){
ob_start();
$items_count=0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$product_id = $cart_item['product_id'];
$quantity = $cart_item['quantity'];
if('some conditions'){
$items_count+=1;
}else{
$items_count+=$quantity;
}
}
?>
<a class="cart-toggle" href="<?php echo wc_get_cart_url(); ?>"><i class="d-icon-bag">
<span class="cart-count" style="opacity: 1;"><?php echo $items_count; ?> </span></i>
</a>
<?php
$fragments['a.cart-toggle'] = ob_get_clean();
return $fragments;
}
简化代码
add_filter( 'woocommerce_add_to_cart_fragments', 'add_to_cart_fragment',10,1);
function add_to_cart_fragment($fragments){
ob_start();
global $woocommerce;
$total_items = $woocommerce->cart->cart_contents_count;
?>
<a class="cart-toggle" href="<?php echo wc_get_cart_url(); ?>"><i class="d-icon-bag">
<span class="cart-count" style="opacity: 1;"><?php echo $total_items; ?> </span></i>
</a>
<?php
$fragments['a.cart-toggle'] = ob_get_clean();
return $fragments;
}