2

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 step-by-step version of how to accomplish this. Any answer would be much appreciated.

David Buck
  • 3,752
  • 35
  • 31
  • 35
Lukas_T
  • 279
  • 1
  • 4
  • 15

3 Answers3

2

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

Rohil_PHPBeginner
  • 6,002
  • 2
  • 21
  • 32
  • Hi, Thanks a lot for the suggestion. I have tried the method above however it still allows me to checkout without applying a coupon, what I need is that a customer is unable to complete an order without applying a coupon code – Lukas_T Dec 18 '14 at 13:26
  • Thank You very much for your Help! Please do let me know if you think of something – Lukas_T Dec 18 '14 at 14:11
  • Hay, thanks for the answer just to be sure I add the php code above into functions .php of my child theme or into form-coupon.php? – Lukas_T Dec 19 '14 at 13:27
  • In your child theme's `functions.php` file and please let me know the output as it is not tested. – Rohil_PHPBeginner Dec 19 '14 at 13:46
  • Hay I have tried the above method, it works but only partially. Basically makes you get stuck on the cart page and doesn't let you go to the checkout page. While I am sure this will be helpful to most other readers here , my particular store doesn't use the cart functionality instead relying on hyperlinks to take you to the checkout page is it possible to have the same effect on the checkout page itself i.e. a user can't submit the order unless a code has been entered the same way if say the postcode was missing. Once again many thanks for your help. – Lukas_T Dec 19 '14 at 17:27
  • Try by modifying `if` statement like this : `if(is_checkout())` instead of `if(is_cart() || is_checkout())`. Please let me know the output. – Rohil_PHPBeginner Dec 20 '14 at 03:39
1

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

Amos
  • 11
  • 1
0

Add the following code in functions.php

Require Coupon for Single Product

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
  $targeted_ids = array(37); // The targeted product ids (in this array)
  $coupon_code = 'summer2'; // The required coupon code
  $coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );

  // Loop through cart items
  foreach(WC()->cart->get_cart() as $cart_item ) {
    // Check cart item for defined product Ids and applied coupon
    if( in_array( $cart_item['product_id'], $targeted_ids ) && ! $coupon_applied ) {
      wc_clear_notices(); // Clear all other notices
      // Avoid checkout displaying an error notice
      wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', 
      $cart_item['data']->get_name() ), 'error' );
      break; // stop the loop
    }
  }
}

Replace 37 by the your product ID in $targeted_ids = array(37); you can have multiple product IDs like $targeted_ids = array(37,48,12);

Replace "summer2" by any other coupon code in $coupon_code = 'summer2'; Don't forget to add this coupon code in WooCommerce before using it.


Require coupon for all products

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
  $product_categories = array( 'clothing' ); // Category ID or slug of targeted category
  $coupon_code = 'summer2'; // The required coupon code
  $coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
  // Loop through cart items
  foreach ( WC()->cart->get_cart() as $cart_item ){
    if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) && !$coupon_applied ) {
      wc_clear_notices(); // Clear all other notices
      // Avoid checkout displaying an error notice
      wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', 
      $cart_item['data']->get_name() ), 'error' );
      break; // stop the loop
    }
  }
}

Replace $product_categories = array( 'clothing' ); by any other category name or category ID.