Created
February 20, 2024 06:39
-
-
Save pramodjodhani/5e6b28623ce57b35c5b799cbf45497fb to your computer and use it in GitHub Desktop.
WDS - disable dates until 1st June when specific products are in cart
This file contains 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 // Dont add this link to functions.php | |
/** | |
* Iconic WDS - Disable all dates for specific product. | |
* | |
* @param array $available_dates Available dates. | |
* @param string $format Date format. | |
* @param bool $ignore_slots Ignore slots. | |
* | |
* @return array | |
*/ | |
function iconic_wds_disable_all_dates_for_specific_product( $available_dates, $format, $ignore_slots ) { | |
if ( 'array' === $format || empty( WC()->cart ) ) { | |
return $available_dates; | |
} | |
// Todo: add list of product IDs in this array. | |
$for_products = array( 10 ); | |
// Check if any of the $for_products products are in the cart. | |
$has_product = false; | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
$product = $cart_item['data']; | |
if ( in_array( $product->get_id(), $for_products ) ) { | |
$has_product = true; | |
break; | |
} | |
} | |
if ( ! $has_product ) { | |
return $available_dates; | |
} | |
// If $for_products products are in the cart, remove dates before 1st March. | |
$filtered_dates = array(); | |
foreach ( $available_dates as $date ) { | |
$date_obj = DateTime::createFromFormat( $format, $date ); | |
$first_march = DateTime::createFromFormat( 'Ymd', '20240601' ); | |
if ( $date_obj >= $first_march ) { | |
$filtered_dates[] = $date; | |
} | |
} | |
return $filtered_dates; | |
} | |
add_filter( 'iconic_wds_available_dates', 'iconic_wds_disable_all_dates_for_specific_product', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment