2

I need to do extra step in the backend when a product is add to cart. I need to get the product ID juste after it's add to the cart.

I use the woocommerce hook woocommerce_add_to_cart

add_action('woocommerce_add_to_cart', 'attach_item');

function attach_item() {
    // I need to have the product id here.
}

I tried many way to get the ID but nothing work. Any idea ...

MathieuB
  • 505
  • 2
  • 8
  • 20

2 Answers2

2

Today I also get the same problem but I got the solution.

add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart',10,2 );

function woo_custom_add_to_cart( $cart_item_data,$productId ) {
    var_dump($productId);
}
Suman Singh
  • 1,379
  • 12
  • 20
0

At the time of writing this, the hook is actually called from WooCommerce like so: (source)

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

Add your custom method like so:

add_action('woocommerce_add_to_cart', function($cartItemKey, $productId) {
  // do sth with $productId
}, 10, 2);
kero
  • 10,647
  • 5
  • 41
  • 51