WooCommerce validate coupon by expiry date and time WooCommerce validate coupon by expiry date and time wordpress wordpress

WooCommerce validate coupon by expiry date and time


This can be made in a more simple and effective way. You can use the following instead:

add_filter( 'woocommerce_coupon_validate_expiry_date', 'filter_coupon_validate_expiry_date', 10, 3 );function validate_coupon( $valid, $coupon, $discount ) {    $expiry_date     = $coupon->get_date_expires();    $expiry_time     = $coupon->get_meta('expiry_time'); // Coupon custom field     if ( $expiry_date && ! empty($expiry_time) ) {        $timezone        = $coupon->get_date_expires()->getTimezone(); // get timezone        $expiry_datetime = new WC_DateTime( $expiry_date->date('Y-m-d') . ' ' . $expiry_time );        $now_datetime    = new WC_DateTime();            $expiry_datetime->setTimezone( $timezone ); // set time zone        $now_datetime->setTimezone( $timezone ); // set time zone                 $valid = $now_datetime->getTimestamp() > $expiry_datetime->getTimestamp();    }    return $valid;   }

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

For plugins that uses a Class with methods and functions, you will replace:

add_filter( 'woocommerce_coupon_validate_expiry_date', 'filter_coupon_validate_expiry_date', 10, 3 );

with:

add_filter( 'woocommerce_coupon_validate_expiry_date', array($this, 'filter_coupon_validate_expiry_date'), 10, 3 );