Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Soft79

    (@josk79)

    Hi Hutchhouse.

    I don’t own that plugin. I don’t know about the license of Woocommerce Subscriptions, but if the license allows so, can you please send it to kalverdijk at hotmail.com? Then I’ll take a look.

    Thread Starter Hutchhouse

    (@hutchhouse)

    HI josk79,

    I’ll ping over a copy – we can build something bespoke for this but the auto coupon functionality has been a constant on other sites so would prefer not to.

    If it’s something you can do quickly then let me know, otherwise we may look at forking your plugin and doing the integration ourselves.

    John at Hutchhouse

    Plugin Author Soft79

    (@josk79)

    Ok, I see… The coupon isn’t applied because my plugin thinks it has no value.

    I released development version 2.1.0-b2 that applies a filter you can use for your case. Please try it and use this snippet in your functions.php :

    add_filter('wjecf_coupon_has_a_value', function($has_a_value, $coupon) {
    	if ( ! $has_a_value ) {
    		//Test if a value applies for WooCommerce Subscriptions plugin
    		//See class-wc-subscriptions-cart.php function cart_coupon_discount_amount_html
    		if ( class_exists( 'WC_Subscriptions_Cart' ) && WC_Subscriptions_Cart::cart_contains_subscription() ) {
    			global $woocommerce;
    			if ( isset( $woocommerce->cart->recurring_coupon_discount_amounts[ $coupon->code ] ) && $woocommerce->cart->recurring_coupon_discount_amounts[ $coupon->code ] > 0 ) {
    				$has_a_value = true;
    			}
    		}
    	}
    	return $has_a_value;
    }, 10, 2 );

    Please let me know if this works for you. I will implement the filter in the next release.

    Thread Starter Hutchhouse

    (@hutchhouse)

    I’ve given that a try but it doesn’t work still. I’m not returning either of the params in the filter ($has_a_value or $coupon).

    It works as a basket discount but just not a s a recurring one..

    Any other ideas?

    Plugin Author Soft79

    (@josk79)

    You tried it in combination with the dev version 2.1.0-b2? $has_a_value should become true and then the discount will be applied.

    It works in my test environment, do you have an environment where I can help you set things up?

    Thread Starter Hutchhouse

    (@hutchhouse)

    I used the new dev release (it has the auto silent option) and recreated the coupon just incase. The params do display in a var_dump – the coupon is there but the boolean continues to return false before it is returned.

    Thread Starter Hutchhouse

    (@hutchhouse)

    The site currently running on our dev environment which is local but the I have added the var_dump for you below:

    object(WC_Coupon)#5409 (20) { ["code"]=> string(15) "firstregiongold" ["id"]=> int(210) ["exists"]=> bool(true) ["discount_type"]=> string(13) "recurring_fee" ["coupon_amount"]=> string(3) "250" ["individual_use"]=> string(2) "no" ["product_ids"]=> array(1) { [0]=> string(2) "57" } ["exclude_product_ids"]=> array(0) { } ["usage_limit"]=> string(0) "" ["usage_limit_per_user"]=> string(0) "" ["limit_usage_to_x_items"]=> string(0) "" ["usage_count"]=> string(0) "" ["expiry_date"]=> string(0) "" ["free_shipping"]=> string(2) "no" ["product_categories"]=> array(0) { } ["exclude_product_categories"]=> array(0) { } ["exclude_sale_items"]=> string(2) "no" ["minimum_amount"]=> string(0) "" ["maximum_amount"]=> string(0) "" ["customer_email"]=> array(0) { } }

    Plugin Author Soft79

    (@josk79)

    Ok, I suggest you the following:

    – Check if the wjecf_coupon_has_a_value-hook is actually called (e.g. placing die('yes it is called'); )
    – Place return true; at the beginning of the hook, does it apply the coupon then? If not something else is wrong.
    – If so, you might have to change the contents of the hook, maybe use get_post_meta to check for discount_fee = recurring_fee or something like that.

    good luck, if you need any help just let me know.

    Thread Starter Hutchhouse

    (@hutchhouse)

    It’s being called because the params are being dumped – we’ll check out and debug. Thanks for your help.

    Thread Starter Hutchhouse

    (@hutchhouse)

    Hi,

    We have now successfully applied the coupon but it could certainly be done better. We were not getting a value in the $woocommerce->cart->recurring_coupon_discount_amounts array so we are now forcing this value to be added per cart item that requires it. In our case the filter looks like this:

    add_filter('wjecf_coupon_has_a_value', 'recurring_autocoupon', 10, 2 );
    
    function recurring_autocoupon($has_a_value, $coupon)
    {
    
        if ( ! $has_a_value ) {
            //Test if a value applies for WooCommerce Subscriptions plugin
            //See class-wc-subscriptions-cart.php function cart_coupon_discount_amount_html
            if ( class_exists( 'WC_Subscriptions_Cart' ) && WC_Subscriptions_Cart::cart_contains_subscription() ) {
    
                global $woocommerce;
    
                if ( $woocommerce->cart->has_discount( $coupon->code ) ) return true;
    
                $woocommerce->cart->recurring_coupon_discount_amounts[ $coupon->code ];
                $has_a_value = true;
            }
        }
        return $has_a_value;
    }

    Can you see anything wrong with this approach or do you think we are good to go?

    Plugin Author Soft79

    (@josk79)

    I don’t think using has_discount doesn’t really make sense.

    I think this would be better:

    add_filter('wjecf_coupon_has_a_value', 'recurring_autocoupon', 10, 2 );
    
    function recurring_autocoupon($has_a_value, $coupon)
    {
    
        if ( ! $has_a_value ) {
    		//Test if a value applies for WooCommerce Subscriptions plugin
    		//See class-wc-subscriptions-cart.php function cart_coupon_discount_amount_html
    		if (in_array( $coupon->type, array( 'sign_up_fee', 'sign_up_fee_percent', 'recurring_fee', 'recurring_percent' ) ) ) {
    			if ( class_exists( 'WC_Subscriptions_Cart' ) && WC_Subscriptions_Cart::cart_contains_subscription() ) {
    				$has_a_value = true;
    			}
    		}
    	}
        return $has_a_value;
    }

    First it tests if it’s a subscription discount, then it checks if the cart contains a subscription product.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Not working with recurring discounts’ is closed to new replies.