Skip to content

Instantly share code, notes, and snippets.

@pramodjodhani
Created October 14, 2022 04:51
Show Gist options
  • Save pramodjodhani/0c911f9d7a04acd9ffe681138c0cce61 to your computer and use it in GitHub Desktop.
Save pramodjodhani/0c911f9d7a04acd9ffe681138c0cce61 to your computer and use it in GitHub Desktop.
Iconic Delivery slots (WDS) define specific for a category
<?php
/**
* Iconic WDS - Set specific dates for a product category.
*
* @param array $dates
* @param string $format
* @param bool $ignore_slots
*
* @return array
*/
function iconic_wds_modify_bookable_dates( $dates, $format, $ignore_slots ) {
// @todo replace 'christmas-pack' with your category.
if ( ! iconic_check_for_cart_item_in_category( 'christmas-pack' ) || 'array' === $format ) {
return $dates;
}
$modified_dates = array(
'12/15/2022',
'12/16/2022',
'12/17/2022',
'12/18/2022',
'12/19/2022',
'12/20/2022',
'12/21/2022',
'12/22/2022',
'12/23/2022',
);
foreach ( $modified_dates as $date ) {
$available_dates[] = date_i18n( $format, strtotime( $date ) );
}
return $available_dates;
}
add_filter( 'iconic_wds_available_dates', 'iconic_wds_modify_bookable_dates', 10, 3 );
/**
* Check cart for product in category.
* * @param array $categories Categories.
* * @return bool
*/
function iconic_check_for_cart_item_in_category( $categories = array() ) {
if ( empty( $categories ) ) {
return false;
}
// Set our flag to be false until we find a product in that category.
$has_item = false;
// Check each cart item for our category.
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
if ( has_term( $categories, 'product_cat', $product->get_id() ) ) {
$has_item = true;
// Break because we only need one "true" to matter here.
break;
}
}
return $has_item;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment