Last active
December 17, 2024 15:00
-
-
Save pramodjodhani/f88a523aecf0a40467545cff06aab05f to your computer and use it in GitHub Desktop.
Iconic WDS - Disable specific dates for specific products.
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 | |
/** | |
* Iconic WDS - Disable specific dates for specific products. | |
* | |
* @param array $available_dates Available dates. | |
* @param string $format Date format. | |
* @param bool $ignore_slots Ignore slots. | |
* | |
* @return array | |
*/ | |
function iconic_wds_disable_specific_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( 177 ); | |
// 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']; | |
$product_id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id(); | |
if ( in_array( $product_id, $for_products ) ) { | |
$has_product = true; | |
break; | |
} | |
} | |
if ( ! $has_product ) { | |
return $available_dates; | |
} | |
// If $for_products products are in the cart, remove dates after 21st December. | |
$filtered_dates = array(); | |
foreach ( $available_dates as $date ) { | |
$date_obj = DateTime::createFromFormat( $format, $date ); | |
$first_march = DateTime::createFromFormat( 'Ymd', '20241221' ); | |
if ( $date_obj < $first_march ) { | |
$filtered_dates[] = $date; | |
} | |
} | |
return $filtered_dates; | |
} | |
add_filter( 'iconic_wds_available_dates', 'iconic_wds_disable_specific_dates_for_specific_product', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment