1
votes

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?

2

2 Answers

1
votes

The following code will Ajax refresh your shortcode custom product category "72" item count:

// Custom conditional function that checks also for parent product category terms
function has_product_category( $product_id, $category_ids, $taxonomy = 'product_cat' ) {
    $term_ids = array(); // Initializing

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $term_ids[] = $term->parent; // Set the parent product category
            $term_ids[] = $term->term_id;
        } else {
            $term_ids[] = $term->term_id;
        }
    }
    return array_intersect( $category_ids, array_unique($term_ids) );
}

// Custom function that count cart items remaining to a product_category
function get_bottiglie_count( $term_ids ){
    $count = 0; // Initializing

    // Loop through each cart item
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( has_product_category( $cart_item['product_id'], $term_ids ) ) {
            $count += $cart_item['quantity'];
        }
    }

    return $count;
}

// Ajax refressing count
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_bottiglie_count', 50, 1 );
function refresh_bottiglie_count( $fragments ){

    $fragments['#bottiglie-count'] = do_shortcode( "[bottiglie]" );

    return $fragments;
}

// Shortcode that display the count cart items remaining to a product_category
add_shortcode('bottiglie', 'shortcode_bottiglie_count');
function shortcode_bottiglie_count( $atts ) {
    return '<span id="bottiglie-count">' . get_bottiglie_count( array(72) ) . '</span>';
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

0
votes

You will be needing a javascript to run a live changes to DOM, but wordpress shortcodes don't work with ajax (I think, correct me if I'm wrong). But yeah, basically you need ajax to fetch the shortcode again or manually change the DOM after clicking add to cart.