[Support request] How to display real-time updated product quantity in shopping cart icon

Home Forums Support [Support request] How to display real-time updated product quantity in shopping cart icon

Home Forums Support How to display real-time updated product quantity in shopping cart icon

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #2336588
    xinghui

    I wanted my cart icon to show the quantity of the product added, so I searched for the usage of generatepress, there was a post that had a code snippet with a custom cart quantity, I added this snippet to the code, then I added it to the text using the button, and replaced the button text with the shortcode. I added and updated a product on the product page, but found that the shopping cart icon did not display the number of products. What is the reason for this? Is there something wrong with my code? ? What should I do?
    This is the post link of generatepress that I refer to:https://generatepress.com/forums/topic/cart-icon-quantity-display/

    #2336656
    Fernando
    Customer Support

    Hi Xinghui,

    Can you provide the link to the site in question?

    You may use the Private Information field for this: https://docs.generatepress.com/?s=private+information

    #2336661
    xinghui

    here is my site link

    #2336804
    David
    Staff
    Customer Support

    Hi there,

    try adding the [db_cart_count] shortcode inside a Headline Block – does it then display ?

    #2336947
    xinghui

    Sorry, I just sent the code wrong, I used this code you wrote
    code:
    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’);

    #2336966
    xinghui

    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:
    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’);
    and this is my problem image url:https://i.postimg.cc/WzHX8FG9/ADD-CART-BUTTON.jpg
    please help me check it ,thank you!

    #2336982
    David
    Staff
    Customer Support

    I said:

    try adding the [db_cart_count] shortcode inside a Headline Block

    If that doesn’t work test it in a Shortcode Block

    #2337024
    xinghui

    Hi,David According to what you said, I tested the shortcode on a product page, added a product to the shopping cart, the quantity still does not show
    problem link:
    https://i.postimg.cc/P5WyCvZm/Screen-Shot-2022-09-08-at-07-05-37.png
    https://i.postimg.cc/sxC9fVXn/Screen-Shot-2022-09-08-at-07-06-03.png

    #2337028
    David
    Staff
    Customer Support
    #2337222
    xinghui

    my add code step:
    step 1.I added a new snippet directly in Snippets and then used Run snippet everywhere, here is a screenshot of my snippet
    image:

    step 2:I created a new element type is block, below is my screenshot
    image:
    step 3:I added a button to the element block, added an icon icon to the button, and replaced the button text with the shortcode I called
    image:
    step 4:My element block display rules
    image:
    step 5:After the settings are saved, go to add products to see if the product count is displayed

    #2337493
    Fernando
    Customer Support

    Can you try this code?

    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');
    add_filter('wp_nav_menu_items', 'do_shortcode');

    Let us know once you’ve added it and we’ll check from our end as well.

    #2338011
    xinghui

    i try your code,but still not display product count,please help me to check

    #2338244
    David
    Staff
    Customer Support

    As a test, try adding the [db_cart_count] to the content of a post. Does it display anything then ?
    If it doesn’t then there is some code on your site that is blocking the shortcodes.

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.