Archived topic
How to display real-time updated product quantity in shopping cart icon
12 replies · Started by xinghui on September 7, 2022
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/
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
here is my site link
Hi there,
try adding the [db_cart_count] shortcode inside a Headline Block - does it then display ?
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');
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!
I said:
try adding the [db_cart_count] shortcode inside a Headline Block
If that doesn't work test it in a Shortcode Block
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
This function:
How did you add that to your site ?
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
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.
i try your code,but still not display product count,please help me to check
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.