WC_Discounts::validate_coupon_product_ids
Ensure coupon is valid for products in the list is valid or throw exception.
Method of the class: WC_Discounts{}
No Hooks.
Returns
true|false.
Usage
// protected - for code of main (parent) or child class $result = $this->validate_coupon_product_ids( $coupon );
- $coupon(WC_Coupon) (required)
- Coupon data.
Changelog
| Since 3.2.0 | Introduced. |
WC_Discounts::validate_coupon_product_ids() WC Discounts::validate coupon product ids code WC 10.6.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;
}
}
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;
}