WordPress at Your Fingertips
function is not described

WC_Discounts::validate_coupon_excluded_product_categories() protected WC 3.2.0

Exclude categories from product list.

{} It's a method of the class: WC_Discounts{}

No Hooks.

Return

true|false.

Usage

// protected - for code of main (parent) or child class
$result = $this->validate_coupon_excluded_product_categories( $coupon );
$coupon(WC_Coupon) (required)
Coupon data.

Changelog

Since 3.2.0 Introduced.

Code of WC_Discounts::validate_coupon_excluded_product_categories() WC 5.6.0

protected function validate_coupon_excluded_product_categories( $coupon ) {
	if ( count( $coupon->get_excluded_product_categories() ) > 0 ) {
		$categories = array();

		foreach ( $this->get_items_to_validate() as $item ) {
			if ( ! $item->product ) {
				continue;
			}

			$product_cats = wc_get_product_cat_ids( $item->product->get_id() );

			if ( $item->product->get_parent_id() ) {
				$product_cats = array_merge( $product_cats, wc_get_product_cat_ids( $item->product->get_parent_id() ) );
			}

			$cat_id_list = array_intersect( $product_cats, $coupon->get_excluded_product_categories() );
			if ( count( $cat_id_list ) > 0 ) {
				foreach ( $cat_id_list as $cat_id ) {
					$cat          = get_term( $cat_id, 'product_cat' );
					$categories[] = $cat->name;
				}
			}
		}

		if ( ! empty( $categories ) ) {
			/* translators: %s: categories list */
			throw new Exception( sprintf( __( 'Sorry, this coupon is not applicable to the categories: %s.', 'woocommerce' ), implode( ', ', array_unique( $categories ) ) ), 114 );
		}
	}

	return true;
}