3

I would like to restrict a coupon code "XYZ" usage only for defined weekdays, from Monday to Thursday only. For other days (friday to sunday) it will show an error notice.

Is it possible? How can I achieve that?

Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Ravi Shankar
  • 141
  • 3
  • 10

1 Answers1

12

Here is a complete solution with 2 hooked functions that will restrict coupon usage for your defined week days and will display a custom error notice if coupon is not valid.

1) Checking "defined days" coupon validity:

add_filter( 'woocommerce_coupon_is_valid', 'coupon_week_days_check', 10, 2);
function coupon_week_days_check( $valid, $coupon ) {

    // Set HERE your coupon slug   <===  <===  <===  <===  <===  <===  <===  <===  <===  <===  
    $coupon_code_wd = 'xyz';
    // Set HERE your defined invalid days (others: 'Mon', 'Tue', 'Wed' and 'Thu')  <===  <===
    $invalid_days = array('Fri', 'Sat', 'Sun');

    $now_day = date ( 'D' ); // Now day in short format

    // WooCommerce version compatibility
    if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
        $coupon_code = strtolower($coupon->code); // Older than 3.0
    } else {
        $coupon_code = strtolower($coupon->get_code()); // 3.0+
    }

    // When 'xyz' is set and if is not a week day we remove coupon and we display a notice
    if( $coupon_code_wd == $coupon_code && in_array($now_day, $invalid_days) ){
        // if not a week day
        $valid = false;
    }
    return $valid;
}

2) Displaying custom error message for "defined days" coupon if not valid:

add_filter('woocommerce_coupon_error', 'coupon_week_days_error_message', 10, 3);
function coupon_week_days_error_message( $err, $err_code, $coupon ) {

    // Set HERE your coupon slug   <===  <===  <===  <===  <===  <===  <===  <===  <===  <===  
    $coupon_code_wd = 'xyz';
    // Set HERE your defined invalid days (others: 'Mon', 'Tue', 'Wed' and 'Thu')  <===  <===
    $invalid_days = array('Fri', 'Sat', 'Sun');

    $now_day = date ( 'D' ); // Now day in short format

    // WooCommerce version compatibility
    if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
        $coupon_code = strtolower($coupon->code); // Older than 3.0
    } else {
        $coupon_code = strtolower($coupon->get_code()); // 3.0+
    }

    if( $coupon_code_wd == $coupon_code && intval($err_code) === WC_COUPON::E_WC_COUPON_INVALID_FILTERED && in_array($now_day, $invalid_days) ) {
        $err = __( "Coupon $coupon_code_wd only works on weekdays and has been removed", "woocommerce" );
    }
    return $err;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This working code is tested on WooCommerce versions 2.6.x and 3.0+.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Code is working good for restrictions but when i am trying to use coupon "xyz" on sunday and i removed 'Sun' from your code then still error showing as "coupon not valid". – Ravi Shankar May 14 '17 at 14:29
  • @ LoicTheAztec; Already i tried by removing 'Sun' from the code but still same issue i am facing as 'coupon is not valid'. I tried with new and older version of woocommerce also but problem is same. Not a single coupon is working. All saying as 'coupon is not valid'. How to overcome with this issue? – Ravi Shankar May 14 '17 at 17:35
  • @RaviShankar **Updated my code!** … I just forgot to return **`$valid`** argument at the end in the first hooked function (and all days have to begin by an Uppercase in a short format (3 characters)). Sorry then. Please try it again It works… – LoicTheAztec May 14 '17 at 19:40
  • @ LoicTheAztec; Thanks , its working fine.One more complexity as, those user only got discounted price whose booking day and service Taken / Delivery day is between Monday To Thursday. e.g : If user select delivery date 21/05/2017 (Sunday) then the coupon not to be apply & if he or she is taken service on 22/05/2017(Monday) then coupon to be apply. – Ravi Shankar May 15 '17 at 12:08
  • I have added your code to snippets and activated it for a monday sale and it works great thank you, now how do I add one for Tuesday? when i copied and pasted the monday information and just changed the coupon code it didn't work – Myra Fletcher Nov 25 '21 at 05:56