Ajax add to cart bug?
-
Ajax add to cart for single product works perfectly, if i add quantity button (getting template from single product page) in the product archive, it even works there but it doesn’t work in any other pages, is there a way to activate on any page?
-
This topic was modified 5 years, 3 months ago by
hellpunch.
-
This topic was modified 5 years, 3 months ago by
-
Hey @hellpunch,
Do you mean that you want to make the AJAX add to cart work on other pages like Cart page?Let me know exactly on what pages you want it to work.
Hello!
Yes but i don’t have any specific page. Can’t i just call it to make it work on x page?
My specific case was that it didn’t work on woocommerce shortcuts of specific categories that were put on other pages than the archive and single product pages.
The fact that it only works on two pages isn’t a limitation wrote by you?
Yes, at the moment the AJAX add to cart work only on archive pages and on single product pages. But we plan to add this functionality on other pages too so that’s why we wanted to see your use case, maybe we will be able to cover it too.
Isn’t the ajax code already there? My case is that i am overwriting the overall add to cart button (woocommerce_template_loop_add_to_cart) to get quantity fields but, in any other page than the two mentioned, ajax is disabled.
@hellpunch yes, the AJAX code is there. Two things:
1. I just tried to list some products on a custom page and the AJAX Add to cart worked there. Can you confirm that you’ve tried this and it didn’t worked for you?
2. Blocksy is responsible only for the AJAX add to cart inside the single product page. Everything else that concerns product cards comes from WooCommerce, so the[products]shortcodes are implemented by core WooCommerce codePlease let me know if you still can’t get your AJAX add to cart on your custom made pages — I’ll try to help.
Cheers!
Hey, thanks for the patience.
1) How did you list those products? I mean, using what? The ajax button on woocommerce gutenberg editor works, true, if i activate the default woocommerce ajax; that of the archive pages, but i don’t know how to add a quantity field so the problem isn’t solved, just moved (and it isn’t theme related so not a concern to you)
2) yes, as i wrote, i was using shortcodes ( not woocommerce gutenberg ) to display specific categories, but, on a custom page, the ajax doesn’t work (is quite bugged actually, redirects you to the product page when you click add to cart) but the same ajax works on archive page, even disabling the default wooommerce archive pages ajax, with this small code (*).
This was all done not using any page builder. Then I tried using britzy template to make woocommerce ‘pages’, but even there, it doesn’t work (probably uses woocommerce shortodes)
Basically what i have is this; i change the ‘add to cart’ from any template loop (archive and any other) with the single product ‘add to cart’, where there is the quantity field, with this code:
(*) remove_action( ‘woocommerce_after_shop_loop_item’, ‘woocommerce_template_loop_add_to_cart’ ); // remove from archive page and any product cards
add_action( ‘woocommerce_after_shop_loop_item’, ‘woocommerce_template_single_add_to_cart’, 30 ); // add just the quantity one
This makes it possible to use your single product ajax even on archive pages without enabling the woocommerce one but the same ajax doesn’t works on other pages. What is that i am missing?
@creativethemeshq You are right in this regard, i didn’t notice. The default woocommerce ajax works even with shortcodes (only default shortcodes). So, we agree, it isn’t theme related but that wasn’t the case of this thread.
The case was that, with this small piece of code ;
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' ); add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_single_add_to_cart', 30 );In shop archive pages, YOUR ajax works even when woocommerce default ajax is DISabled. But is buggy when using shortcodes, even though i do get the quantity field of the single product page.
I think i understand now that shortcodes are handled differently and need another type of implementation of ajax perhaps? and your single product ajax only works for single product pages and shop archive pages.
Well in this case, if you want to help me further else but, the ajax issue is kinda solved. I’ll try to figure out how to add both the quantity input and make shortcode work with ajax. Thanks! (Don’t limit your ajax to only single product pages, leave as it is now so it can work on archive pages too, thanks!)
-
This reply was modified 5 years, 3 months ago by
hellpunch.
-
This reply was modified 5 years, 3 months ago by
hellpunch.
-
This reply was modified 5 years, 3 months ago by
hellpunch.
-
This reply was modified 5 years, 3 months ago by
hellpunch.
-
This reply was modified 5 years, 3 months ago by
hellpunch.
-
This reply was modified 5 years, 3 months ago by
hellpunch.
-
This reply was modified 5 years, 3 months ago by
hellpunch.
-
This reply was modified 5 years, 3 months ago by
hellpunch.
-
This reply was modified 5 years, 3 months ago by
hellpunch.
-
This reply was modified 5 years, 3 months ago by
hellpunch.
-
This reply was modified 5 years, 3 months ago by
hellpunch.
-
This reply was modified 5 years, 3 months ago by
hellpunch.
@hellpunch got your point — you added the quantity in the archives pages and the custom page with the shortcode too. Noted, will try to get this case to work.
-
This reply was modified 5 years, 3 months ago by
Creative Themes.
@creativethemeshq Hey, Thanks! It already works on archive pages, it was just for shortcodes.
-
This reply was modified 5 years, 3 months ago by
hellpunch.
@hellpunch I see now what you mean. So for our AJAX add to cart to be triggered, the
<body>tag needs to have thect-ajax-add-to-cartclass. This class is added if the option is enabled in the customizer and if we are on a single product page or in an WooCommerce archive. That’s why you custom page isn’t covered.Just add this code in your
functions.phpfile of the child theme:add_filter('body_class', function ($classes) { // Or, if you want, just add an if statement to check if you are on the needed page here $classes[] = 'ct-ajax-add-to-cart'; return $classes; });Hope this helps!
Much appreciated, thanks x1000
-
This reply was modified 5 years, 3 months ago by
The topic ‘Ajax add to cart bug?’ is closed to new replies.
