I'm using riode theme. I need to change the total cart items count shows in the header menu mini cart icon. I've 2 types of products. 1 is weight-based other is normal. e.g.: My cart items => Product1 - 35gm - $50.00 ; Product2 - 2(items) - $30.00 ; here I need the total cart items to count as 3. but now it shows 37.
Here is my code.
<?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;
}
CodePudding user response:
global $woocommerce;
$total_items = $woocommerce->cart->cart_contents_count;
CodePudding user response:
Simplify your code
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;
}