Следующий код Ajax обновит ваш шорткод пользовательской категории продуктов "72" Количество элементов:
// 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>';
}
Код находится в файле function.php вашей активной дочерней темы (активной темы).Проверено и работает.