Reputation: 123
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.
Upvotes: 8
Views: 23619
Reputation: 166
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
Upvotes: 15
Reputation: 578
This is not a direct answer for the question, its more a solution his use case scenario.
You can just create a FIFO
queue somewhere in your system that handle all slack notifications sends.
The scenario would be like:
using this technique to isolate action that may cause delay in response time like notification send emails.
Upvotes: 0