woocommerce get product id when product is add to cart

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

Read More
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 …

Related posts

Leave a Reply

2 comments

  1. 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);
    }
    
  2. 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);