1

I would like to know whether an addition of a product to a cart occurred on woocommerce. On the documentation it says that an application should consume a webhook that notifies about a woocommerce_add_to_cart action.

So I implemented it and gladly got a message saying it happened a short while after such an event occurred.

Here's an example JSON I got from woocommerce:

{"action":"woocommerce_add_to_cart","arg":"fe9fc289c3ff0af142b6d3bead98a923"}

However since many visitors of a woocommerce website may have their own cart, and many products, I would like to know if there's a way to decode the arg field so that it will provide a unique product that was added to a specific cart.

In case I am using the wrong method in order to learn about an addition to a cart, I would be happy to learn which one may be better for me.

1 Answer 1

0

The 'arg' value is the cart item key that is passed through woocommerce_add_to_cart action hook as an argument, and that is returned on WC_Cart add_to_cart() method on line 1120:

do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );

return $cart_item_key; // <========= HERE

That cart item key allows you for example to get the cart item using WC_Cart get_cart_item() method and retrieve the cart item data:

$cart_item = WC()->cart->get_cart_item( $cart_item_key );
1
  • 1
    Thanks for your thorough reply LoicTheAztec :-) Now, I get the arg field within a java program that accepts webhooks notifications, so extracting a cart item or the whole cart info is not something that I know how to reach within my program. Do you know of a way to reach this info from a REST client?
    – javaman
    Apr 14, 2019 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.