I created a shortcode that counts the quantity of products belonging to the '72' product category, that are added to the cart.
I am using the custom conditional function has_product_category() from this answer thread:
Set item quantity to multiples of “x” for products in a specific category in Woocommerce
Here is my code:
function cat_cart_count_bottiglie() {
$bottiglie = 0;
// Iterating through each cart item
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_product_category( $cart_item['product_id'], $category_ids = array( 72 ) ) ) {
$bottiglie += $cart_item['quantity'];
}
}
return $bottiglie;
}
add_shortcode('bottiglie', 'cat_cart_count_bottiglie');
The code works correctly.
But this shortcode count get updated only when the page is refreshed after add to cart and doesn't work for Ajax add to cart or when removing items or changing items quantity in cart page.
Is there a way to get the update instantly?