Last active
February 24, 2026 09:38
-
-
Save thisissandip/c3bfa39f1c2fba6de6904f7688c18cee to your computer and use it in GitHub Desktop.
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 | |
| /** | |
| * Replace existing booking in cart with new one | |
| * | |
| * When a customer adds a new booking for a specific bookable product, | |
| * any existing booking for the same product is automatically removed. | |
| * This ensures cross-resource blocking works correctly since it triggers on payment. | |
| */ | |
| // Define the bookable product ID (update after creating the product) | |
| define('SINGLE_CART_BOOKING_PRODUCT_ID', 123); // Replace with actual product ID | |
| function replace_existing_booking_in_cart($passed, $product_id, $quantity) { | |
| // Only apply to the specified bookable product | |
| if ($product_id != SINGLE_CART_BOOKING_PRODUCT_ID) { | |
| return $passed; | |
| } | |
| if (!WC()->cart) { | |
| return $passed; | |
| } | |
| // Find and remove any existing bookings for this product from cart | |
| $removed = false; | |
| foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { | |
| if ($cart_item['product_id'] == SINGLE_CART_BOOKING_PRODUCT_ID) { | |
| WC()->cart->remove_cart_item($cart_item_key); | |
| $removed = true; | |
| } | |
| } | |
| // Notify customer that their previous booking was replaced | |
| if ($removed) { | |
| wc_add_notice( | |
| __('Your previous booking has been replaced with the new selection.', 'woocommerce'), | |
| 'notice' | |
| ); | |
| } | |
| return $passed; | |
| } | |
| add_filter('woocommerce_add_to_cart_validation', 'replace_existing_booking_in_cart', 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment