WC_Brands_Coupons::is_coupon_valid()
Validate the coupon based on included and/or excluded product brands.
If one of the following conditions are met, an exception will be thrown and displayed as an error notice on the cart page:
1) Coupon has a brand requirement but no products in the cart have the brand.
2) All products in the cart match the brand exclusion rule.
3) For a cart discount, there is at least one product in cart that matches exclusion rule.
Метод класса: WC_Brands_Coupons{}
Хуков нет.
Возвращает
true|false
. $valid True if coupon is valid, otherwise Exception will be thrown.
Использование
$WC_Brands_Coupons = new WC_Brands_Coupons(); $WC_Brands_Coupons->is_coupon_valid( $valid, $coupon, $discounts );
- $valid(true|false) (обязательный)
- Whether the coupon is valid.
- $coupon(WC_Coupon) (обязательный)
- Coupon object.
- $discounts(WC_Discounts)
- Discounts object.
По умолчанию: null
Код WC_Brands_Coupons::is_coupon_valid() WC Brands Coupons::is coupon valid WC 9.6.1
public function is_coupon_valid( $valid, $coupon, $discounts = null ) { $this->set_brand_settings_on_coupon( $coupon ); // Only check if coupon has brand restrictions on it. $brand_coupon_settings = WC_Brands_Brand_Settings_Manager::get_brand_settings_on_coupon( $coupon ); $brand_restrictions = ! empty( $brand_coupon_settings['included_brands'] ) || ! empty( $brand_coupon_settings['excluded_brands'] ); if ( ! $brand_restrictions ) { return $valid; } $included_brands_match = false; $excluded_brands_matches = 0; $items = $discounts->get_items(); foreach ( $items as $item ) { $product_brands = $this->get_product_brands( $this->get_product_id( $item->product ) ); if ( ! empty( array_intersect( $product_brands, $brand_coupon_settings['included_brands'] ) ) ) { $included_brands_match = true; } if ( ! empty( array_intersect( $product_brands, $brand_coupon_settings['excluded_brands'] ) ) ) { ++$excluded_brands_matches; } } // 1) Coupon has a brand requirement but no products in the cart have the brand. if ( ! $included_brands_match && ! empty( $brand_coupon_settings['included_brands'] ) ) { throw new Exception( $coupon->get_coupon_error( WC_Coupon::E_WC_COUPON_NOT_APPLICABLE ), WC_Coupon::E_WC_COUPON_NOT_APPLICABLE ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } // 2) All products in the cart match brand exclusion rule. if ( count( $items ) === $excluded_brands_matches ) { throw new Exception( __( 'Sorry, this coupon is not applicable to the brands of selected products.', 'woocommerce' ), self::E_WC_COUPON_EXCLUDED_BRANDS ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } // 3) For a cart discount, there is at least one product in cart that matches exclusion rule. if ( $coupon->is_type( 'fixed_cart' ) && $excluded_brands_matches > 0 ) { throw new Exception( __( 'Sorry, this coupon is not applicable to the brands of selected products.', 'woocommerce' ), self::E_WC_COUPON_EXCLUDED_BRANDS ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } return $valid; }