Created
February 10, 2026 15:56
-
-
Save thisissandip/4a0411108c76f6a5a084f1eb3f7a510a to your computer and use it in GitHub Desktop.
WooCommerce Bookings: Bookings date based coupon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Booking Date-Based Coupon Rules | |
| * | |
| * Define which coupons are valid for which booking date ranges. | |
| * Just edit the $coupon_rules array below. | |
| */ | |
| add_filter( 'woocommerce_coupon_is_valid', function( $valid, $coupon, $discount ) { | |
| if ( ! $valid || ! WC()->cart ) { | |
| return $valid; | |
| } | |
| // ============================================ | |
| // DEFINE YOUR COUPON RULES HERE | |
| // ============================================ | |
| $coupon_rules = array( | |
| // Coupon code => array( 'from' => 'YYYY-MM-DD', 'to' => 'YYYY-MM-DD' ) | |
| 'MARCH2026' => array( 'from' => '2026-03-01', 'to' => '2026-03-31' ), | |
| 'SUMMER2026' => array( 'from' => '2026-06-01', 'to' => '2026-08-31' ), | |
| 'SPRING2026' => array( 'from' => '2026-03-20', 'to' => '2026-06-20' ), | |
| // Add more coupons here... | |
| ); | |
| $coupon_code = strtoupper( $coupon->get_code() ); | |
| if ( ! isset( $coupon_rules[ $coupon_code ] ) ) { | |
| return $valid; // No rules for this coupon | |
| } | |
| $rules = $coupon_rules[ $coupon_code ]; | |
| $from_date = strtotime( $rules['from'] ); | |
| $to_date = strtotime( $rules['to'] ); | |
| foreach ( WC()->cart->get_cart() as $cart_item ) { | |
| if ( empty( $cart_item['booking']['_start_date'] ) ) { | |
| continue; // Not a booking product | |
| } | |
| $booking_start = strtotime( date( 'Y-m-d', $cart_item['booking']['_start_date'] ) ); | |
| if ( $booking_start < $from_date || $booking_start > $to_date ) { | |
| wc_add_notice( sprintf( | |
| 'Coupon "%s" is only valid for bookings between %s and %s.', | |
| $coupon->get_code(), | |
| date( 'M j, Y', $from_date ), | |
| date( 'M j, Y', $to_date ) | |
| ), 'notice' ); | |
| return false; | |
| } | |
| } | |
| return $valid; | |
| }, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment