简体   繁体   中英

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.

Answering this old question for Google Searchers:

The action woocommerce_add_to_cart is fired after an item is added to the cart. https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html#1118

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

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:

  1. when a new item is added to the cart, you create a simple queue record for it (eg: AWS SQS, DB table record, etc. etc.)
  2. using a cron-job, you execute some sort of code that will read the entries from your queue and perform the "send notification".

using this technique to isolate action that may cause delay in response time like notification send emails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM