Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce added to cart hook (after the product was successfully added to cart)

I need a hook that fires after a product was added to cart. Something like a "woocommerce_add_to_cart" successful callback.

I imagine it would be something like "woocommerce_added_to_cart", but couldn't find anything like that. I know there is an ajax event "added_to_cart" but it will be a lot of code for me to do that via ajax.

Update - my use case: I am making slack notifications on my wc shop. All of my "Add to cart" buttons are ajax, and it takes approximatively 0.6 seconds from clicking "Add to cart" until it appears in the cart. If I add my slack notification with "woocommerce_add_to_cart" hook then it waits until it delivers the notification to slack and then updates the cart which is up to 2 seconds, which is too much. Best case scenario would be to have a php hook that fires after the product was successfully added to cart, which will not affect it.

like image 593
Gaina Roman Avatar asked Aug 02 '17 14:08

Gaina Roman


People also ask

How do I trigger add to cart button in WooCommerce?

Yes, that can be done by selecting the Redirect user to a URL option in your form's Behavior > Submission Behavior settings. Change yoursite. tld to your actual site, and change 7283 to the ID of the product you want added to the user's cart.

How do I add a Add to cart button to my WordPress product page?

Go to Appearance -> Customize, then go to WooCommerce -> Add to Cart Buttons to choose your settings. Change the Add To Cart button text and/or select the other options on this screen.

How add to cart works in WooCommerce?

Adding products to your shopping cart in WooCommerce is easy! Simply find the product you want to purchase, and then click the “Add to Cart” button. The product will be added to your shopping cart, and you can continue shopping or proceed to checkout.

How do I add a hook to WooCommerce?

To use WooCommerce hooks (or WordPress hooks in general), you'll need to add code to your site. But again, you do not need to edit the template files themselves – you can add this code all in the same spot. There are two places you can add this code: Your child theme's functions.


1 Answers

Answering this old question for Google Searchers:

The action woocommerce_add_to_cart is fired after an item is added to the cart.

and can be used like:

add_action( 'woocommerce_add_to_cart', function ()
{
  // your code here
});

related action woocommerce_cart_item_removed fires after an item is removed

like image 179
Tristan Avatar answered Sep 30 '22 20:09

Tristan