woocommerce_add_to_cart хук-событиеWC 2.5.0

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.

Использование

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(строка)
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 of variation data.
$cart_item_data(массив)
Array of other cart item data.

Список изменений

С версии 2.5.0 Введена.

Где вызывается хук

CartController::add_to_cart()
woocommerce_add_to_cart
WC_Cart::add_to_cart()
woocommerce_add_to_cart
woocommerce/src/StoreApi/Utilities/CartController.php 165-173
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 1286
do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );

Где используется хук в WooCommerce

woocommerce/includes/class-wc-cart-session.php 78
add_action( 'woocommerce_add_to_cart', array( $this, 'persistent_cart_update' ) );
woocommerce/includes/class-wc-cart-session.php 84
add_action( 'woocommerce_add_to_cart', array( $this, 'maybe_set_cart_cookies' ) );
woocommerce/includes/class-wc-cart.php 106
add_action( 'woocommerce_add_to_cart', array( $this, 'calculate_totals' ), 20, 0 );
woocommerce/includes/class-wc-form-handler.php 875
remove_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );
woocommerce/includes/class-wc-form-handler.php 882
add_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );