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

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