Cart count / total price

Davor Mlinarić
16 May 2022

With this example you can easily add cart count or total price anywhere on your webshop. This code uses woocommerce add to chart hook that is executed every time when you put something into basket.

You just need to add this code anywhere where you wish to show total price.

<div class="webbox-cart-count"><?php echo WC()->cart->get_cart_total(); ?></div>

And this snippet will execute every time when somebody add item to basket and will replace content above div with proper value. In this case it will show total price.

add_filter( 'woocommerce_add_to_cart_fragments', 'webbox_cart_count_fragments', 10, 1 );

function webbox_cart_count_fragments( $fragments ) {
    
    $count = WC()->cart->get_cart_contents_count(); // items in basket
    $price = WC()->cart->get_cart_total(); // total price

    $fragments['div.webbox-cart-count'] = '<div class="webbox-cart-count">' . $price .  '</div>';

    return $fragments;
    
}

Was this article helpful?

Tags

Categories

Never miss new post again

Subscribe and get list of new posts in your inbox

Click to Copy