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

Topic Resolution: Resolved
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #6077
    Yulia Kolesnik
    Participant

    Hi, despite that plugin functions correctly and does amazing things I am having an issue:
    Free products that are to be chosen by customer are not deducted from the stock when gift option is not chosen.
    For example: I give an option to choose 1 of 4 shirts when cart is over 50$, if customer forgets to choose his gift, coupon still applies to his order, but without a free product gift, therefore that gift-product quantity is no being reduced in stock. Is there a way to display cart error message with “Please choose your gift” reminder before checkout?

    #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.

    #6101
    Yulia Kolesnik
    Participant

    It does show free products on the checkout page, but:
    1) no error message when gift is not selected
    2) and most importantly – doesn’t proceed to PayPal even if gift is 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.

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