Home › Forums › Basel support forum › Hook on Add to Product and problem with ajax add to cart
Hook on Add to Product and problem with ajax add to cart
- This topic has 13 replies, 2 voices, and was last updated 8 years, 1 month ago by Artem Temos.
-
AuthorPosts
-
November 9, 2016 at 5:01 pm #6484
HumanParticipantHi,
I’ve hooked a function into ‘woocommerce_after_add_to_cart_button’ action to do something. everything is fine except when I want to add a product to my cart via AJAX add to cart button and ‘woocommerce_after_add_to_cart_button’ does not trigger.
What should I do?
November 9, 2016 at 7:04 pm #6489
Artem TemosKeymasterHello,
Could you please explain what do you mean by ‘woocommerce_after_add_to_cart_button’ does not trigger? It is a hook and you can add some text or PHP blocks to it.
Regard
November 9, 2016 at 8:29 pm #6495
HumanParticipantI want to do some extra stuff after a user added a product to cart by clicking Ajax add to cart button? Which hook should I use?
November 9, 2016 at 9:09 pm #6496
Artem TemosKeymasterIn this case, you need to use JS
added_to_cart
event. The code will look like this$('body').bind('added_to_cart', function(event, fragments, cart_hash) { alert('added to cart'); });
Regards
November 9, 2016 at 9:23 pm #6498
HumanParticipantHow can I call a function in my functions.php after one product added to cart via Ajax add to cart?
November 9, 2016 at 9:35 pm #6499
Artem TemosKeymasterWe have posted an example of you can hook this event with JS, not PHP. So you need to include some new JS file in your child theme and write your code there.
Regards
November 9, 2016 at 9:55 pm #6501
HumanParticipantLet me describe my problem in better words:
1- A user goes to product page detail and click on add to cart button.
2- Selected product will add to cart and along this my custom function runs:
function rg_add_rugpad_to_cart() { // Action... } add_filter('woocommerce_after_add_to_cart_button', 'rg_add_rugpad_to_cart', 12);
3- Another user goes to shop page and hover his mouse pointer on product thumbnail and Add to cart button will be shown.
4- He clicks on this hover button and selected product added to cart using AJAX, oh wait! this time my custom rg_add_rugpad_to_cart() function was not called! but It must be!
So how can I solve my problem?
November 9, 2016 at 10:04 pm #6509
Artem TemosKeymasterBut there is no AJAX add to cart on the product page, right? AJAX exists only on the shop page (category listing). But
woocommerce_after_add_to_cart_button
is shown on the product page right after add to cart button. So it is not clear for us how your customer can add the product to cart with AJAX on the single product page?Regards
November 9, 2016 at 10:08 pm #6512
HumanParticipantPlease read item number 3 in my previous post carefully. I’ve mentioned to shop page.
November 9, 2016 at 10:09 pm #6514
Artem TemosKeymasterSorry, we see now. But the problem is that hook you are using
woocommerce_after_add_to_cart_button
comes from WooCommerce and it is shown only on the single product page.November 9, 2016 at 10:15 pm #6518
HumanParticipantI need to add some extra free products to cart upon every addition to cart. so what should I do to make it happen both from AJAX add to cart and Add to Cart on single product page?
I don’t want to use any available plug-in for this purpose.
November 9, 2016 at 10:18 pm #6520
Artem TemosKeymasterUnfortunately, we don’t have a quick instruction for you regarding your question. It is more related to WooCommerce functionality and it is out of our support scope. But we would be glad to help you if you have any issue with our theme or see that some code is not working only with Basel but works with standard WordPress theme.
Thank you for understanding.
Regards
November 9, 2016 at 10:21 pm #6521
HumanParticipantWhat’s the name of PHP file that handle Ajax add to cart button posted Items in Basel?
November 9, 2016 at 10:23 pm #6522
Artem TemosKeymasterThis function comes from WooCommerce, not our theme. You can read more about it here https://docs.woocommerce.com/wc-apidocs/class-WC_AJAX.html
Regards
-
AuthorPosts
- You must be logged in to create new topics. Login / Register