wcmenucart does not appear on ajax add to cart when cart was empty
-
Hello,
I have noticed that the wcmenucart menu does not appear when adding a product to cart through Ajax. It only appears when refreshing the page. It would be best if the menu item was always added to the menu and hid when it’s empty, instead of adding it conditionally.
Please fix. Thank you!
-
Here is a possible fix:
diff --git a/wp-content/themes/oceanwp/inc/woocommerce/woocommerce-helpers.php b/wp-content/themes/oceanwp/inc/woocommerce/woocommerce-helpers.php index 67af8e9..6eb2c18 100644 --- a/wp-content/themes/oceanwp/inc/woocommerce/woocommerce-helpers.php +++ b/wp-content/themes/oceanwp/inc/woocommerce/woocommerce-helpers.php @@ -19,7 +19,9 @@ if ( ! function_exists( 'oceanwp_wcmenucart_menu_item' ) ) { // Return items if "hide if empty cart" is checked (for mobile) if ( true == get_theme_mod( 'ocean_woo_menu_icon_hide_if_empty', false ) && ! WC()->cart->cart_contents_count > 0 ) { - return; + $hide_class = 'wcmenucart-hide'; + } else { + $hide_class = ''; } // Return if is in the Elementor edit mode, to avoid error @@ -75,7 +77,7 @@ if ( ! function_exists( 'oceanwp_wcmenucart_menu_item' ) ) { // If bag style if ( 'yes' == get_theme_mod( 'ocean_woo_menu_bag_style', 'no' ) ) { ?> - <a href="<?php echo esc_url( $url ); ?>" class="wcmenucart"> + <a href="<?php echo esc_url( $url ); ?>" class="wcmenucart <?php echo $hide_class; ?>"> <?php if ( true == get_theme_mod( 'ocean_woo_menu_bag_style_total', false ) ) { ?> <span class="wcmenucart-total"><?php echo WC()->cart->get_cart_total(); ?></span> @@ -87,7 +89,7 @@ if ( ! function_exists( 'oceanwp_wcmenucart_menu_item' ) ) { <?php } else { ?> - <a href="<?php echo esc_url( $url ); ?>" class="wcmenucart"> + <a href="<?php echo esc_url( $url ); ?>" class="wcmenucart <?php echo $hide_class; ?>"> <span class="wcmenucart-count"><?php echo wp_kses_post( $cart_icon ); ?><?php echo wp_kses_post( $cart_extra ); ?></span> </a>
diff --git a/wp-content/themes/oceanwp/inc/woocommerce/woocommerce-config.php b/wp-content/themes/oceanwp/inc/woocommerce/woocommerce-config.php index aecec55..1d14f4d 100644 --- a/wp-content/themes/oceanwp/inc/woocommerce/woocommerce-config.php +++ b/wp-content/themes/oceanwp/inc/woocommerce/woocommerce-config.php @@ -1708,11 +1708,12 @@ if ( ! class_exists( 'OceanWP_WooCommerce_Config' ) ) { return $items; } + /* Not needed anymore // Return items if "hide if empty cart" is checked if ( true == get_theme_mod( 'ocean_woo_menu_icon_hide_if_empty', false ) && ! WC()->cart->cart_contents_count > 0 ) { return $items; - } + }*/ // Add cart link to menu items if ( 'full_screen' == $header_style ) {
And add this to assets/css/woo/woocommerce.css:
.wcmenucart-hide, #mobile-fullscreen ul li a.wcmenucart-hide { display: none; }
I hope this can be fixed. Thank you!
-
This reply was modified 6 years, 10 months ago by
therealgilles.
-
This reply was modified 6 years, 10 months ago by
therealgilles.
Hi Chamois,
Thanks for following up with this! Theme developer will check this and execute if verifies.
Thank you!
Hello, can you show me the issue? I didn’t totally understand.
Hi, sorry for not getting back to you earlier, let me try to explain again.
I have a site where I show the mobile menu hamburger at all times. I use the Woocommerce extension One Page checkout, although I don’t think it has anything to do with my problem.
When I click to add a product to the cart, the menu item that shows the number of items in the cart and the cart total does not show up. It only shows up on page refresh. This happens because of the code issues I described above.
Am I making sense?
Hello, if you have disabled any scripts, go to Theme Panel > Scripts & Styles, click Save Changes, then clear your cache, tell me if it solves the issue?
Thank you for your reply.
I have not disabled any scripts. I am not quite understanding your reply. I showed above the code that’s creating the issue and gave a proposal as to how to fix it. I do not see how it could work as is, that is without the changes I proposed. The cart menu item cannot be made to appear if it’s not added through the php code when the cart is empty.
Let me know if I am misunderstanding something.
Oh sorry, I think I have understood the issue now ??
You want to display the cart when a product is added to it without refreshing the page, that’s right?Yes, that’s correct. That’s the behavior that makes the most sense. It gives the user instant feedback that the product was added to the cart.
Hello, I tried your code but it cannot work as you expect because the class is added only when the page is reloaded so the result is the same.
Let me look, maybe I forgot to give you another change I made.
It is working on my site with the above changes. You can see it in action on this video.
What class are you referring to? wcmenucart-hide is added by default by the first code change (woocommerce_helpers.php) when the cart is empty. The change to woocommerce-config.php ensures the menu item is always included.
Hi, can you directly send me the theme that you have edited? I miss something, it doesn’t work on my site.
Thank you.I’m an idiot ??
I forgot to remove the “return;” it was the problem.
Thank you very much for the code, now it works perfectly.I thought about asking you that ?? Hurray!
??
Feel free to open a new topic if you find something else to improve, much appreciated ?? -
This reply was modified 6 years, 10 months ago by
- The topic ‘wcmenucart does not appear on ajax add to cart when cart was empty’ is closed to new replies.