Cart Date Hook for Coupon rerstriction
-
Hi there!
I am in the process of helping a small leisure rental company with an issue regarding coupons in conjunction with a booking from Pinpoint.
They would like to create coupons valid only for bookings on weekdays (Monday/Thursday).
A Pinpoint booking will continue to be handled as a Woocommerce product. So are the coupons.
Now I have a code that works with Woocommerce booking, but these hooks are different in the shopping cart.
Will you help me make this code correct using the right hook(s)? The code is shown below.
// Coupon validity checking add_filter('woocommerce_coupon_is_valid', 'coupon_week_days_check', 10, 2); function coupon_week_days_check($valid, $coupon) { // Set the coupon codes you want to restrict to weekdays $coupon_codes = array('couponcode', 'anothercode', 'thirdcode'); $coupon_code = strtolower($coupon->get_code()); $found = false; // Loop through cart items to check if the coupon code matches and the date is a weekday foreach (WC()->cart->get_cart() as $cart_item) { if (in_array($coupon_code, $coupon_codes) && isset($cart_item['booking']['_date'])) { $the_day = date('D', strtotime($cart_item['booking']['_date'])); if (in_array($the_day, array('Mon', 'Tue', 'Wed', 'Thu'))) { $found = true; break; // No need to continue checking once we find a matching weekday } } } // If the coupon code is not valid for weekdays, set it as invalid if (!$found) { $valid = false; } return $valid; } // Coupon validity checking error message add_filter('woocommerce_coupon_error', 'coupon_weekdays_error_message', 10, 3); function coupon_weekdays_error_message($err, $err_code, $coupon) { // Set the coupon codes you want to restrict to weekdays $coupon_codes = array('couponcode', 'anothercode', 'thirdcode'); $coupon_code = strtolower($coupon->get_code()); if (intval($err_code) === WC_COUPON::E_WC_COUPON_INVALID_FILTERED && in_array($coupon_code, $coupon_codes)) { $err = __("Code $coupon_code is uitsluitend te gebruiken voor boekingen op ma/do.", "woocommerce"); } return $err; }
In this, I think the combination hooks [‘booking’][‘_date’] is the culprit; but I can’t find the right hooks(s) in the source code.
I have already tried these alternatives:
[‘reservation’][‘_date’]
[‘appointmend’][‘_date’]
Thanks in advance!The page I need help with: [log in to see the link]
- The topic ‘Cart Date Hook for Coupon rerstriction’ is closed to new replies.