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

#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
);