You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce Booking:基于预订使用日期限制周末优惠券使用

WooCommerce Bookings: Restrict Coupons by Booking Date (Not Order Date)

Alright, let's tackle this problem head-on — since WooCommerce's default coupon system only checks the order date (when the customer completes checkout) instead of the actual booking date (when they'll use the service), we need a custom code tweak to override this behavior. Here's a practical, tailored solution for your use case (coupons valid only for weekday bookings, blocked for weekends like Saturday):

Core Approach

We'll hook into WooCommerce's coupon validation flow, pull the actual booking date from the cart items, check if it falls on a weekday (Monday-Friday), and reject the coupon if it's a weekend day.

Custom Code Snippet

Add this to your child theme's functions.php file, or use a code snippet plugin (like Code Snippets) to avoid losing changes when updating your theme:

add_filter( 'woocommerce_coupon_is_valid', 'restrict_coupon_to_weekday_bookings', 10, 2 );
function restrict_coupon_to_weekday_bookings( $is_valid, $coupon ) {
    // Skip this check in admin (avoids conflicts with backend order management)
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return $is_valid;
    }

    // Loop through cart items to find booking products
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Verify the item is a WooCommerce Booking
        if ( isset( $cart_item['booking'] ) && $cart_item['booking'] instanceof WC_Booking ) {
            $booking = $cart_item['booking'];
            // Get the booking's start date (use 'end_date' if you need to check the end instead)
            $booking_date = $booking->get_start_date( 'Y-m-d' );
            // Get day of week: 1 = Monday, 5 = Friday, 6 = Saturday, 0 = Sunday
            $day_of_week = date( 'w', strtotime( $booking_date ) );

            // Block coupon if booking is on Saturday or Sunday
            if ( $day_of_week == 0 || $day_of_week == 6 ) {
                wc_add_notice( __( 'This coupon cannot be used for weekend bookings.', 'woocommerce' ), 'error' );
                return false;
            }
        }
    }

    // If all bookings are on weekdays, keep the coupon valid
    return $is_valid;
}

Customization & Testing Tips

  • Target Specific Coupons: If you only want this rule for certain coupons, add a check at the start of the function:
    // Only apply to coupon with code 'WEEKDAY_SPECIAL'
    if ( $coupon->get_code() !== 'WEEKDAY_SPECIAL' ) {
        return $is_valid;
    }
    
  • Holiday Blocking: Want to exclude holidays too? Create an array of holiday dates (in Y-m-d format) and add a check to see if $booking_date matches any entry.
  • Test Thoroughly:
    1. Create a test coupon in WooCommerce.
    2. Add a Saturday booking to your cart — applying the coupon should trigger the error message.
    3. Repeat with a weekday booking — the coupon should apply without issues.

内容的提问来源于stack exchange,提问作者Adrián Estoup

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:15:08