WC_Brands_Coupons::is_valid_for_productpublicWC 1.0

Check if a coupon is valid for a product.

This allows percentage and product discounts to apply to only the correct products in the cart.

Method of the class: WC_Brands_Coupons{}

No Hooks.

Returns

true|false. $valid

Usage

$WC_Brands_Coupons = new WC_Brands_Coupons();
$WC_Brands_Coupons->is_valid_for_product( $valid, $product, $coupon );
$valid(true|false) (required)
Whether the product should get the coupon's discounts.
$product(WC_Product) (required)
WC Product Object.
$coupon(WC_Coupon) (required)
Coupon object.

WC_Brands_Coupons::is_valid_for_product() code WC 10.5.0

public function is_valid_for_product( $valid, $product, $coupon ) {

	if ( ! is_a( $product, 'WC_Product' ) ) {
		return $valid;
	}
	$this->set_brand_settings_on_coupon( $coupon );

	$product_id     = $this->get_product_id( $product );
	$product_brands = $this->get_product_brands( $product_id );

	// Check if coupon has a brand requirement and if this product has that brand attached.
	$brand_coupon_settings = WC_Brands_Brand_Settings_Manager::get_brand_settings_on_coupon( $coupon );
	if ( ! empty( $brand_coupon_settings['included_brands'] ) && empty( array_intersect( $product_brands, $brand_coupon_settings['included_brands'] ) ) ) {
		return false;
	}

	// Check if coupon has a brand exclusion and if this product has that brand attached.
	if ( ! empty( $brand_coupon_settings['excluded_brands'] ) && ! empty( array_intersect( $product_brands, $brand_coupon_settings['excluded_brands'] ) ) ) {
		return false;
	}

	return $valid;
}