Repository Forums Support WooCommerce Extended Coupon Features PRO Force free product coupon with single use coupon

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #15509
    anthonyceo
    Participant

    I want to be able to stack single use coupons with free product coupons. For example, if someone enters a 20% off coupon, I still want them to be able to get a free item.

    Is there options for this or a filter I can use to do this?

    #15513
    Soft79
    Keymaster

    You can use the following WooCommerce filters for that:

    woocommerce_apply_individual_use_coupon (must return an array with already applied coupons that may be kept)
    woocommerce_apply_with_individual_use_coupon (must return a boolean whether a coupon can be applied while there are individual use coupons in the cart)

    #15515
    anthonyceo
    Participant

    Thank you.

    filter_woocommerce_apply_individual_use_coupon( $array, $the_coupon, $this_applied_coupons )

    What is $array for?

    Is info whether or not a coupon has free products stored in $the_coupon?
    (e.g. $the_coupon->free_products or something like that)

    #15516
    Soft79
    Keymaster

    It’s the coupons to keep when the individual use coupon is applied. E.g. return all the auto coupons that are already applied to the cart.

    See https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html#1582

    #15544
    anthonyceo
    Participant

    Thank you for your help. 🙂

    Here’s the simple code for others trying to do the same thing. Unfortunately, the coupon object doesn’t declare whether the coupon has a free item. However, all of our coupons for Free Products have a discount amount of 0.

    add_filter('woocommerce_apply_with_individual_use_coupon', 'force_bogo_coupon', 10, 2);
    
    function force_bogo_coupon($bool, $coupon) {
    	$amount = $coupon->amount;
    	$bool = ($amount == 0) ? true : $bool;
    	return $bool;
    }

    Edit:

    this code is more specific to a free product:

    add_filter('woocommerce_apply_with_individual_use_coupon', 'force_bogo_coupon', 10, 2);
    
    function force_bogo_coupon($bool, $coupon) {
    	$meta = $coupon->get_meta('_wjecf_must_select_free_product');
    	$bool = ($meta === 'yes') ? true : $bool;
    	return $bool;
    }
    #15709
    Yulia Kolesnik
    Participant

    This works like a charm, thank you! I’ve been looking for exactly this solution.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.