WC_Discounts::validate_coupon_sale_itemsprotectedWC 3.2.0

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

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

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

Возвращает

bool.

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

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

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

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

Код WC_Discounts::validate_coupon_sale_items() WC 11.1.2

protected function validate_coupon_sale_items( $coupon ) {
	if ( $coupon->get_exclude_sale_items() ) {
		$valid = true;

		foreach ( $this->get_items_to_validate() as $item ) {
			if ( $item->product && $item->product->is_on_sale() ) {
				$valid = false;
				break;
			}
		}

		/**
		 * Filter whether the coupon is valid for the cart given its sale_items 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 given the sale items in the cart.
		 * @param WC_Coupon    $coupon    Coupon data.
		 * @param WC_Discounts $discounts The discounts instance.
		 */
		$valid = apply_filters( 'woocommerce_coupon_is_valid_for_sale_items', $valid, $coupon, $this );

		if ( ! $valid ) {
			throw new Exception(
				sprintf(
					/* translators: %s: coupon code */
					esc_html__( 'Sorry, coupon "%s" is not valid for sale items.', 'woocommerce' ),
					esc_html( $coupon->get_code() )
				),
				110
			);
		}
	}

	return true;
}