Site logo

Archived topic

Cart Icon (Quantity Display)

9 replies · Started by Juned on October 18, 2021

Viewing posts 1–10 of 10

Hi,

I followed this trick of Ying and implemented it sucessfully at Mobile view: https://generatepress.com/forums/topic/footer-sticky-bar/#post-1669570

(pls check in Mobile view)

Now I need your help for: When added a product to basket, the sticky-bottom button's icon should also display the quantity. Is it possible?

Just like in this Mobil view (private):

Hi there,

try this:

1. Add this snippet to create a shortcode:

function db_custom_cart_count() {

    ob_start();

	if ( ! class_exists( 'WooCommerce' ) ) {
		return;
	}

	if ( ! isset( WC()->cart ) ) {
		return;
	}

	$no_items = '';

	if ( ! WC()->cart->get_cart_contents_count() > 0 ) {
		$no_items = 'no-items';
	}

	printf(
        '<span class="cart-contents"><span class="number-of-items %1$s">%2$s</span></span>',
        $no_items,
        WC()->cart->get_cart_contents_count()
    );
    return ob_get_clean();
}
add_shortcode('db_cart_count', 'db_custom_cart_count');

2. Edit your button label and add: [db_cart_count]

Hello,

Thanks for your response.

1. I added the snippet via Code Snippets --> Run snippet everywhere.
2. Where should I add "[db_cart_count]"?
At button, I can see: HTML Anchor | ARIA Label | Additional CSS class(es) | Custom Attributes.

Please suggest where should I put this: [db_cart_count]

(I have linked Cart Page URL also with the button)

Thanks in advance.

You add it as the button label, so when you select the button and you would normally add the button text, you add it in there. Make sure the Button text isn't set to hidden.

Awesome. As always.

:)

Glad to be of help!

Hello, David, I added the short code to the button text as you said, but it still does not display, the custom code is the one you wrote before that shows the number of products that I used directly
code :
if ( has_term( 'dummy', 'product_tag' ) ) {

// Get Parent Category and its count
$taxonomy = 'product_cat';
$cat = get_the_terms( $post->ID, $taxonomy )[0];
$count = (int) $cat->count;

// Loop through children categories
$child_cats = get_term_children( $cat->term_id, $taxonomy);
foreach ($child_cats as $child_cat) {
$term = get_term( $child_cat, $taxonomy );
$count +=$term->count;
}

printf(
'%2$s <span class="counter">(%3$s)<span>',
$catLink = get_term_link( $cat , $taxonomy ),
$catName = $cat->name,
$count
);

}
my problem image :

Hi David,

thank you very much for this great solution. I just have two questions:
1. Is there a way to blend out the number count if nothing is in the cart?
2. The count is not updated as long as the page is not reloaded. How can I add AJAX update? In WooCommerce->Product->General the hook for AJAX support is set.

Thanks a lot in advance!

Hi there,

I don't have a shortcode that supports Ajax.

if you require Ajax then you would need to use the GP Premium option to add the menu cart to the Navigation.
It can be set in Customizer > Layout > Woocommerce. And it has an option to set it to display the Number of Items.

This archived topic is closed to new replies.