Making the coupon field mandatory on WooCommerce

微笑、不失礼 提交于 2020-01-03 03:45:30

问题


I was wondering if it was possible to make the coupon field mandatory on Woocommerce.

I know that this is possible using functions, however this is slightly above my current skill level so I was wondering if you could give me a dumbed-down/step-by-step version of how to accomplish this. Any answer would be much appreciated.

or is there perhaps a plugin that can help me accomplish this?


回答1:


I don't know about the function but you can modify the plugin to achieve this in following manner :

Make one folder in your theme folder woocommerce and in new created woocommerce folder, create another folder with checkout name.

So now it will look something like wp-content > themes > your-theme > woocommerce > checkout.

Now go to your plugin directory and follow below path :

wp-content > plugins > woocommerce > templates > checkout

When you go in above path, you will find one file named as form-coupon.php. Copy that file and paste it to the directory which we created at top of that answer.

wp-content > themes > your-theme > woocommerce > checkout > form-coupon.php.

Now its time to modify the code in wp-content > themes > your-theme > woocommerce > checkout > form-coupon.php :

Find following code line in above mentioned file :

<input type="text" name="coupon_code" class="input-text" placeholder="<?php _e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" />

And replace above line with

<input type="text" name="coupon_code" class="input-text" placeholder="<?php _e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" required/>

Note: Here I have added required attribute of html.

Tell me if you have any doubt.

UPDATED:

    add_action('woocommerce_check_cart_items', 'make_coupon_code');

    function make_coupon_code()
    {
        global $woocommerce;
        if(is_cart() || is_checkout()){
            $my_coupon = $woocommerce->cart->applied_coupons;
            echo $woocommerce->cart->get_applied_coupons;
            if(empty($my_coupon))
            {
                $woocommerce->add_error("Please enter coupon code to checkout.");
            }
        }
    }

Please give it a try and let me know feedback.

NOTE: UNTESTED




回答2:


to solve the problem try this code:

    <?php
 add_action('woocommerce_check_cart_items', 'make_coupon_code');

    function make_coupon_code()
    {
        global $woocommerce;
        if(is_cart() || is_checkout()){
            $my_coupon = $woocommerce->cart->applied_coupons;
            echo $woocommerce->cart->get_applied_coupons;
            if(empty($my_coupon))
            {
                wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'insert coupon code', 'woocommerce' ), 'error' );
            }
        }
    }
?>

in functions.php instead of the one above...

for me it works



来源:https://stackoverflow.com/questions/27526677/making-the-coupon-field-mandatory-on-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!