WC_Discounts::validate_coupon_product_idsprotectedWC 3.2.0

Ensure coupon is valid for products in the list is valid or throw exception.

Метод класса: WC_Discounts{}

Хуки из метода

Возвращает

bool.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->validate_coupon_product_ids( $coupon );
$coupon(WC_Coupon) (обязательный)
Coupon data.

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

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

Код WC_Discounts::validate_coupon_product_ids() WC 11.1.2

protected function validate_coupon_product_ids( $coupon ) {
	if ( count( $coupon->get_product_ids() ) > 0 ) {
		$valid = false;

		foreach ( $this->get_items_to_validate() as $item ) {
			if ( $item->product && ( in_array( $item->product->get_id(), $coupon->get_product_ids(), true ) || in_array( $item->product->get_parent_id(), $coupon->get_product_ids(), true ) ) ) {
				$valid = true;
				break;
			}
		}

		/**
		 * Filter whether the coupon is valid for the cart given its product_ids restriction.
		 *
		 * Return true to treat the coupon as valid, or false to reject it. Mind the polarity: these
		 * woocommerce_coupon_is_valid_for_* filters follow the woocommerce_coupon_is_valid convention where
		 * true means valid. This is the opposite of the woocommerce_coupon_validate_* filters
		 * (e.g. woocommerce_coupon_validate_expiry_date), where returning true rejects the coupon.
		 *
		 * @since 11.1.0
		 * @param bool         $valid     Whether the coupon is valid for the selected products.
		 * @param WC_Coupon    $coupon    Coupon data.
		 * @param WC_Discounts $discounts The discounts instance.
		 */
		$valid = apply_filters( 'woocommerce_coupon_is_valid_for_product_ids', $valid, $coupon, $this );

		if ( ! $valid ) {
			throw new Exception(
				sprintf(
				/* translators: %s: coupon code */
					esc_html__( 'Sorry, coupon "%s" is not applicable to selected products.', 'woocommerce' ),
					esc_html( $coupon->get_code() )
				),
				109
			);
		}
	}

	return true;
}