woocommerce_add_cart_item
Filters the item being added to the cart.
Usage
add_filter( 'woocommerce_add_cart_item', 'wp_kama_woocommerce_add_cart_item_filter', 10, 2 ); /** * Function for `woocommerce_add_cart_item` filter-hook. * * @param array $cart_item_data Array of cart item data being added to the cart. * @param string $cart_id Id of the item in the cart. * * @return array */ function wp_kama_woocommerce_add_cart_item_filter( $cart_item_data, $cart_id ){ // filter... return $cart_item_data; }
- $cart_item_data(array)
- Array of cart item data being added to the cart.
- $cart_id(string)
- Id of the item in the cart.
Changelog
Since 2.5.0 | Introduced. |
Where the hook is called
woocommerce_add_cart_item
woocommerce/src/StoreApi/Utilities/CartController.php 138-153
$cart->cart_contents[ $cart_id ] = apply_filters( 'woocommerce_add_cart_item', array_merge( $request['cart_item_data'], array( 'key' => $cart_id, 'product_id' => $this->get_product_id( $product ), 'variation_id' => $this->get_variation_id( $product ), 'variation' => $request['variation'], 'quantity' => $request_quantity, 'data' => $product, 'data_hash' => wc_get_cart_item_data_hash( $product ), ) ), $cart_id );
woocommerce/includes/class-wc-cart.php 1267-1282
$this->cart_contents[ $cart_item_key ] = apply_filters( 'woocommerce_add_cart_item', array_merge( $cart_item_data, array( 'key' => $cart_item_key, 'product_id' => $product_id, 'variation_id' => $variation_id, 'variation' => $variation, 'quantity' => $quantity, 'data' => $product_data, 'data_hash' => wc_get_cart_item_data_hash( $product_data ), ) ), $cart_item_key );