Repository › Forums › Support › WooCommerce Extended Coupon Features PRO › Contact us form – no reply › Reply To: Contact us form – no reply

#20654
Soft79
Keymaster

Hi,

  • Use ‘Categories ‘AND’ and choose the categories to restrict to
  • Set min quantity of matching products to: 2
  • Enable ‘Allow multiplication of the free products’
  • Paste the following snippet to your child theme

NOTE: This will only work if you use a category that contains all collars and one that contains all leads. Otherwise this snippet needs customization.

/**
 * If 'Categories AND' and multiply free products are enabled,
 * limit the amount of free products to the amount a product of each category is in the cart.
 */
add_filter( 
    'wjecf_free_product_amount_for_coupon', 
    function( $coupon_qty, $coupon ) {
        $categories_and = $coupon->get_meta( '_wjecf_categories_and' ) == 'yes';
        if ( ! $categories_and ) return $coupon_qty;
        foreach ( $wc_discounts->get_items() as $item_key => $item ) {
            if ( ! $item->product ) continue;
            $product_id = $item->product->get_id();
            if ( 'product_variation' == get_post_type( $product_id ) ) {
                $product_id = $item->product->get_parent_id();
            }
            $product_cats = array_merge( $product_cats, wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ) );
        }
        //Filter used by WJECF_WPML hook
        $product_cats = apply_filters( 'wjecf_get_product_cat_ids', $product_cats );
        //check if every single category is in the cart
        foreach ( apply_filters( 'wjecf_get_product_cat_ids', $coupon->get_product_categories() ) as $cat_id ) {
            $count = array_count_values( $product_cats, $cat_id );
            $count_qty = min( $coupon_qty, $count );
        }
        return $coupon_qty;
    }, 10, 2
);