Repository Forums Support WooCommerce Extended Coupon Features PRO How to set free gift as required option

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #19492
    gbafnachirpn
    Participant

    Hi, We have purchased pro version of the plugin. we have a condition, where we provide 5 product options and the user MUST select one option to proceed to checkout or else user cannot proceed to checkout. Can you please help with this condition?
    Please look attached screen shot.
    https://drive.google.com/file/d/1t5sSNoqo_SMlxQ2QdSI05LXSERWWlByI/view

    #19495
    Soft79
    Keymaster

    Please try this snippet in your child theme’s functions.php:

    
    add_action( 'woocommerce_before_checkout_process', 
        function() {
            $plugin = WJECF()->get_plugin( 'pro-free-products' );
            if ( ! $plugin ) 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 ( ! $plugin->must_select_free_product( $coupon ) ) {
                    continue;
                }
    
                $session_selected_products = $plugin->get_session_selected_products( $coupon_code );
                if ( empty( $session_selected_products ) ) {
                    wc_add_notice( __('Please select your free gift.', 'woocommerce-jos-autocoupon'), 'error' );
                    return;
                }
            }
        },
        20
    );
    
    #19498
    gbafnachirpn
    Participant

    Hello,
    I updated this code on child theme function.php. No message is displayed when a customer doesn’t select free sample products.
    Please look attached screenshot.
    https://drive.google.com/drive/folders/1L418R2QGDt0Ydbmo5MGqBxbNux8pwMVS

    #19499
    Soft79
    Keymaster

    Oh, sorry there was an error in the script. Try this:

    
    add_action( 'woocommerce_before_checkout_process', 
        function() {
            $plugin = WJECF()->get_plugin( 'pro-free-products' );
            if ( ! $plugin ) 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 ( ! $plugin->must_select_free_product( $coupon ) ) continue;
    
                $session_selected_products = $plugin->get_session_selected_products( $coupon_code );
    
                if ( empty( $session_selected_products ) ) continue;
    
                foreach( $session_selected_products as $item ) {
                    if ( $item->getQuantity() > 0 ) continue 2;
                }
    
                wc_add_notice( __('Please select your free gift.', 'woocommerce-jos-autocoupon'), 'error' );
                return;
            }
        },
        20
    );
    
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.