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

#6097
Soft79
Keymaster

This can be done by pasting a small snippet in your child theme’s functions.php:


add_action( 'woocommerce_before_checkout_process', function() {
    if ( ! is_callable( 'WJECF_API' ) ) 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_API()->must_select_free_product( $coupon ) ) {
            continue;
        }

        $session_selected_products = WJECF_API()->get_session_selected_products( $coupon_code );
        $selected_free_products = WJECF_API()->count_selected_products( $coupon, $session_selected_products );

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

I had to make some untested changes to this snippet. Please let me know if it has issues.