- This topic has 12 replies, 3 voices, and was last updated 6 months, 3 weeks ago by
David.
-
AuthorPosts
-
September 7, 2022 at 11:16 pm #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/September 8, 2022 at 12:53 am #2336656Fernando 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
September 8, 2022 at 1:01 am #2336661xinghui
here is my site link
September 8, 2022 at 3:22 am #2336804David
StaffCustomer SupportHi there,
try adding the
[db_cart_count]
shortcode inside a Headline Block – does it then display ?Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 8, 2022 at 5:50 am #2336947xinghui
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’);September 8, 2022 at 6:22 am #2336966xinghui
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!September 8, 2022 at 6:33 am #2336982David
StaffCustomer SupportI said:
try adding the [db_cart_count] shortcode inside a Headline Block
If that doesn’t work test it in a Shortcode Block
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 8, 2022 at 7:12 am #2337024xinghui
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.pngSeptember 8, 2022 at 7:17 am #2337028David
StaffCustomer SupportThis function:
How did you add that to your site ?
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 8, 2022 at 8:31 am #2337222xinghui
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 displayedSeptember 8, 2022 at 6:15 pm #2337493Fernando 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.
September 9, 2022 at 7:15 am #2338011xinghui
i try your code,but still not display product count,please help me to check
September 9, 2022 at 8:40 am #2338244David
StaffCustomer SupportAs 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.Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.