woocommerce_add_to_cart
Fires when an item is added to the cart.
This hook fires when an item is added to the cart. This is triggered from the Store API in this context, but WooCommerce core add to cart events trigger the same hook.
Usage
add_action( 'woocommerce_add_to_cart', 'wp_kama_woocommerce_add_to_cart_action', 10, 6 ); /** * Function for `woocommerce_add_to_cart` action-hook. * * @param string $cart_id ID of the item in the cart. * @param integer $product_id ID of the product added to the cart. * @param integer $request_quantity Quantity of the item added to the cart. * @param integer $variation_id Variation ID of the product added to the cart. * @param array $variation Array of variation data. * @param array $cart_item_data Array of other cart item data. * * @return void */ function wp_kama_woocommerce_add_to_cart_action( $cart_id, $product_id, $request_quantity, $variation_id, $variation, $cart_item_data ){ // action... }
- $cart_id(string)
- ID of the item in the cart.
- $product_id(int)
- ID of the product added to the cart.
- $request_quantity(int)
- Quantity of the item added to the cart.
- $variation_id(int)
- Variation ID of the product added to the cart.
- $variation(array)
- Array of variation data.
- $cart_item_data(array)
- Array of other cart item data.
Changelog
Since 2.5.0 | Introduced. |
Where the hook is called
woocommerce_add_to_cart
woocommerce/src/StoreApi/Utilities/CartController.php 200-208
do_action( 'woocommerce_add_to_cart', $cart_id, $this->get_product_id( $product ), $request_quantity, $this->get_variation_id( $product ), $request['variation'], $request['cart_item_data'] );
woocommerce/includes/class-wc-cart.php 1287
do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
Where the hook is used in WooCommerce
woocommerce/includes/class-wc-cart-session.php 80
add_action( 'woocommerce_add_to_cart', array( $this, 'persistent_cart_update' ) );
woocommerce/includes/class-wc-cart-session.php 86
add_action( 'woocommerce_add_to_cart', array( $this, 'maybe_set_cart_cookies' ) );
woocommerce/includes/class-wc-cart.php 107
add_action( 'woocommerce_add_to_cart', array( $this, 'calculate_totals' ), 20, 0 );
woocommerce/includes/class-wc-form-handler.php 886
remove_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );
woocommerce/includes/class-wc-form-handler.php 893
add_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );