Created
February 23, 2026 06:10
-
-
Save pramodjodhani/01524bca024903dead2447a55ebb12e0 to your computer and use it in GitHub Desktop.
Restrict Orderable service dates if a specific product is in the cart.
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 | |
| /** | |
| * Restrict Orderable service dates if a specific product is in the cart. | |
| * | |
| * @param array|bool $service_dates The currently available service dates. | |
| * @param string $type The service type ('delivery' or 'pickup'). | |
| * @param Orderable_Location_Single $location The location instance. | |
| * @return array|bool | |
| */ | |
| function orderable_restrict_service_dates( $service_dates, $type, $location ) { | |
| if ( ! is_array( $service_dates ) || empty( $service_dates ) ) { | |
| return $service_dates; | |
| } | |
| // TODO: Define the product ID that triggers the restriction. | |
| $restricted_product_id = 123; // Replace 123 with your actual product ID | |
| $has_restricted_product = false; | |
| if ( ! is_null( WC()->cart ) ) { | |
| foreach ( WC()->cart->get_cart() as $cart_item ) { | |
| if ( in_array( $restricted_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) { | |
| $has_restricted_product = true; | |
| break; | |
| } | |
| } | |
| } | |
| // If the product is not in the cart, return the original dates unaltered. | |
| if ( ! $has_restricted_product ) { | |
| return $service_dates; | |
| } | |
| // Apply the restriction logic. | |
| $filtered_dates = array(); | |
| foreach ( $service_dates as $date ) { | |
| $timestamp = $date['timestamp']; | |
| $day_of_week = date( 'w', $timestamp ); // 0 (Sunday) to 6 (Saturday) | |
| // EXAMPLE LOGIC: Only allow this product to be serviced on Weekends (Saturday and Sunday) | |
| if ( in_array( $day_of_week, array( 0, 6 ) ) ) { | |
| $filtered_dates[] = $date; | |
| } | |
| } | |
| return $filtered_dates; | |
| } | |
| add_filter( 'orderable_location_service_dates', 'orderable_restrict_service_dates', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment