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

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #6484

    Human
    Participant

    Hi,

    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?

    #6489

    Artem Temos
    Keymaster

    Hello,

    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

    #6495

    Human
    Participant

    I 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?

    #6496

    Artem Temos
    Keymaster

    In 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

    #6498

    Human
    Participant

    How can I call a function in my functions.php after one product added to cart via Ajax add to cart?

    #6499

    Artem Temos
    Keymaster

    We 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

    #6501

    Human
    Participant

    Let 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?

    #6509

    Artem Temos
    Keymaster

    But 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

    #6512

    Human
    Participant

    Please read item number 3 in my previous post carefully. I’ve mentioned to shop page.

    #6514

    Artem Temos
    Keymaster

    Sorry, 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.

    #6518

    Human
    Participant

    I 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.

    #6520

    Artem Temos
    Keymaster

    Unfortunately, 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

    #6521

    Human
    Participant

    What’s the name of PHP file that handle Ajax add to cart button posted Items in Basel?

    #6522

    Artem Temos
    Keymaster

    This 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

Viewing 14 posts - 1 through 14 (of 14 total)