79203389

Date: 2024-11-19 12:20:34
Score: 2
Natty:
Report link

i used this code and i think it worked, to have a coupon working only on thursdays but on sunday the client received an order with this coupon and discount. I don´t find the error in this code:

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 = 'xxxxx';
// Set HERE your defined invalid days (others: 'Mon', 'Tue', 'Fri', 'Wed' and 'Thu')  <===  <===
$invalid_days = array('Mon', 'Tue', 'Wed', '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;

}

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 = 'xxxxx';
// Set HERE your defined invalid days (others: 'Mon', 'Tue', 'Wed' and 'Thu')  <===  <===
$invalid_days = array('Mon', 'Tue', 'Wed', '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 = __( "El Cupón $coupon_code_wd solo funciona los jueves", "woocommerce" );
}
return $err;

}

Thanks in advance

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Whitelisted phrase (-1): it worked
  • RegEx Blacklisted phrase (3): Thanks in advance
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Pablo Enlazarte