Repository Forums Support WooCommerce Extended Coupon Features PRO Stock is not reduced when free product is not selected Reply To: Stock is not reduced when free product is not selected

#6103
Soft79
Keymaster

I see, the method unfortunately needs access to a private method. This is a quick and dirty fix:


add_action( 'woocommerce_before_checkout_process', function() {
    if ( ! is_callable( 'WJECF' ) ) return;
    //Don't checkout if no free gift selected
    foreach( WC()->cart->get_applied_coupons() as $coupon_code ) {
        $coupon = new WC_Coupon( $coupon_code );
        //Must the user select a free product?
        if ( ! WJECF()->get_plugin('pro-free-products')->must_select_free_product( $coupon ) ) {
            continue;
        }

        $fp = WJECF()->get_plugin('pro-free-products');

        $session_selected_products = $fp->get_session_selected_products( $coupon_code );

        $reflector = new ReflectionObject( $fp );
        $method = $reflector->getMethod('count_selected_products');
        $method->setAccessible(true);
        $selected_free_products = $method->invoke($fp, $coupon, $session_selected_products );

        if ( empty( $selected_free_products ) ) {
            wc_add_notice( __('Please select your free gift.', 'woocommerce-jos-autocoupon'), 'error' );
            return;
        }
    }
}, 20 );

I’m thinking about adding this functionality into the core plugin. But for now you can use this (dirty) solution.