Automattic\WooCommerce\StoreApi\Utilities

CartController::filter_request_data()protectedWC 1.0

Filter data for add to cart requests.

Method of the class: CartController{}

Return

Array. Updated request array.

Usage

// protected - for code of main (parent) or child class
$result = $this->filter_request_data( $request );
$request(array) (required)
Add to cart request params.

CartController::filter_request_data() code WC 8.7.0

protected function filter_request_data( $request ) {
	$product_id   = $request['id'];
	$variation_id = 0;
	$product      = wc_get_product( $product_id );

	if ( $product->is_type( 'variation' ) ) {
		$product_id   = $product->get_parent_id();
		$variation_id = $product->get_id();
	}

	/**
	 * Filter cart item data for add to cart requests.
	 *
	 * @since 2.5.0
	 *
	 * @internal Matches filter name in WooCommerce core.
	 *
	 * @param array $cart_item_data Array of other cart item data.
	 * @param integer $product_id ID of the product added to the cart.
	 * @param integer $variation_id Variation ID of the product added to the cart.
	 * @param integer $quantity Quantity of the item added to the cart.
	 * @return array
	 */
	$request['cart_item_data'] = (array) apply_filters(
		'woocommerce_add_cart_item_data',
		$request['cart_item_data'],
		$product_id,
		$variation_id,
		$request['quantity']
	);

	if ( $product->is_sold_individually() ) {
		/**
		 * Filter sold individually quantity for add to cart requests.
		 *
		 * @since 2.5.0
		 *
		 * @internal Matches filter name in WooCommerce core.
		 *
		 * @param integer $sold_individually_quantity Defaults to 1.
		 * @param integer $quantity Quantity of the item added to the cart.
		 * @param integer $product_id ID of the product added to the cart.
		 * @param integer $variation_id Variation ID of the product added to the cart.
		 * @param array $cart_item_data Array of other cart item data.
		 * @return integer
		 */
		$request['quantity'] = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $request['quantity'], $product_id, $variation_id, $request['cart_item_data'] );
	}

	return $request;
}