Skip to content

Instantly share code, notes, and snippets.

@btribouillet
Created September 14, 2018 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btribouillet/d6d053ba95434c926451f92b6a51e39e to your computer and use it in GitHub Desktop.
Save btribouillet/d6d053ba95434c926451f92b6a51e39e to your computer and use it in GitHub Desktop.
Feature request (WooCommerce Delivery Slots): Add a filter in slots_available_on_date function
function domain_set_slot_availability( $is_available, $timeslot, $date ) {
// Custom code to set $is_available to false or true.
return $is_available;
}
add_filter( 'iconic_wds_slot_available', 'domain_set_slot_availability', 10, 3 );
/**
* Helper: Get all slots available on a specific date
*
* @param string $date Ymd
*
* @return array
*/
function slots_available_on_date( $date ) {
$timeslots = $this->get_timeslot_data();
$datetime = DateTime::createFromFormat( 'Ymd', $date );
$date_timestamp = $datetime->getTimestamp();
$available_timeslots = array();
if ( ! $timeslots ) {
return $available_timeslots;
}
foreach ( $timeslots as $timeslot ) {
$slot_id = sprintf( '%s_%s', $date, $timeslot['id'] );
$slot_allowed_on_day = $this->is_timeslot_available_on_day( $date_timestamp, $timeslot );
$in_past = $this->is_timeslot_in_past( $timeslot, $date );
$slot_allowed_for_method = $this->is_timeslot_allowed_for_method( $timeslot );
/* START : apply_filters added */
$slot_allowed_filter = apply_filters( 'iconic_wds_slot_available', true, $timeslot, $date );
if ( ! $slot_allowed_on_day || $in_past || ! $slot_allowed_for_method || ! $slot_allowed_filter ) {
continue;
}
/* END: apply_filters added */
$slots_available_count = $this->get_slots_available_count( $timeslot, $date );
if ( $slots_available_count <= 0 ) {
continue;
}
$timeslot['slot_id'] = $slot_id;
$available_timeslots[] = $timeslot;
}
return $available_timeslots;
}
@alasdairmacewan
Copy link

Hi, does this code limit allow for the filtering dates for specific shipping methods, i.e. if the customer picks delivery it blocks out all dates apart from those typed into the code above, i.e. i could setup a xmas shipping method and have it set to only allow for delivery on 22nd,23rd and 24th of December. Any help or input is appreciated, thanks

@btribouillet
Copy link
Author

btribouillet commented Oct 2, 2020

Hi @alasdairmacewan,

This was a feature request for the plugin Iconic delivery slot. They did add the hook / filter but with a different name. iconic_wds_is_timeslot_available_on_day

So the way I'm using it now is like that.


/**
 * Use iconic_wds_is_timeslot_available_on_day to enable/disable timeslot
 *
 * @param bool   $allowed default value from iconic_wds_is_timeslot_available_on_day filter to return.
 * @param string $timestamp default value from iconic_wds_is_timeslot_available_on_day filter.
 * @param array  $timeslot default value from iconic_wds_is_timeslot_available_on_day filter.
 * @return bool
 */
function set_slot_availability( $allowed, $timestamp, $timeslot ) {
        // check which shipping method is selected or available
        // set $allowed to false or true based on the desired shipping method

	return $allowed;
}
add_filter( 'iconic_wds_is_timeslot_available_on_day',  'set_slot_availability' , 10, 3 );

But this will only disable each or a specific timeslot for the selected date.

If you want you can also use iconic_wds_slots_available_on_date hook or iconic_wds_is_day_allowed hook.

I see there is also a hook called iconic_wds_timeslot_shipping_method_allowed. It might worth to look into that.

Let me know if that helps.

@alasdairmacewan
Copy link

Hi, yes it certainly does help, however Im terrible at PHP,
Can you help me with this a little,
I know it in english what I want it to do but just not how to write it in PHP
So basically I want it to be something like this
If shipping method Xmas is selected then show 22 december 23rd december and 24th december and then it should also allow for the time slots to work within this if statement

I think that makes sense on how to approach this problem

Many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment