Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pramodjodhani/51efd930b17654614ff1699db1e0765e to your computer and use it in GitHub Desktop.
Save pramodjodhani/51efd930b17654614ff1699db1e0765e to your computer and use it in GitHub Desktop.
Iconic WDS modify cutoff time and allowed days based on shipping zone
<?php
/**
* Return shipping zones.
*/
function iconic_get_shipping_zones() {
// TODO modify these arrays as per your store setup.
return array(
'zone_1' => array( 'free_shipping:1', 'flat_rate:7' ),
'zone_2' => array( 'flat_rate:3', 'free_shipping:4' ),
'zone_3' => array( 'free_shipping:2' ),
);
}
/**
* Change delivery dates based on the currently selected shipping method.
*
* @param array $allowed_days Allowed days.
*/
function iconic_change_allowed_days( $allowed_days ) {
if ( empty( WC()->session ) ) {
return;
}
global $iconic_wds;
$zones = iconic_get_shipping_zones();
$post_shipping_method = filter_input( INPUT_POST, 'shipping_method', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
$shipping_method = ( ! empty( $post_shipping_method ) && is_array( $post_shipping_method ) ) ? $post_shipping_method[0] : $iconic_wds->get_chosen_shipping_method_for_session();
if ( in_array( $shipping_method, $zones['zone_3'], true ) ) {
return array(
0 => false, // Sunday.
1 => true, // Monday.
2 => true, // Tuesday.
3 => true, // Wednesday.
4 => true, // Thursday.
5 => true, // Friday.
6 => false, // Saturday.
);
}
return $allowed_days;
}
add_filter( 'iconic_wds_allowed_days', 'iconic_change_allowed_days' );
/**
* Iconic WDS: modify same and next day cutoff.
*/
function iconic_change_same_next_day_cutoff() {
if ( empty( WC()->session ) ) {
return;
}
global $iconic_wds;
$zones = iconic_get_shipping_zones();
$post_shipping_method = filter_input( INPUT_POST, 'shipping_method', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
$shipping_method = ( ! empty( $post_shipping_method ) && is_array( $post_shipping_method ) ) ? $post_shipping_method[0] : $iconic_wds->get_chosen_shipping_method_for_session();
if ( in_array( $shipping_method, $zones['zone_1'], true ) ) {
$iconic_wds->settings['datesettings_datesettings_sameday_cutoff'] = '15:00';
$iconic_wds->settings['datesettings_datesettings_nextday_cutoff'] = false; // no cutoff.
}
if ( in_array( $shipping_method, $zones['zone_2'], true ) ) {
$iconic_wds->settings['datesettings_datesettings_sameday_cutoff'] = '00:00';
$iconic_wds->settings['datesettings_datesettings_nextday_cutoff'] = false; // no cutoff.
}
if ( in_array( $shipping_method, $zones['zone_3'], true ) ) {
$iconic_wds->settings['datesettings_datesettings_sameday_cutoff'] = '15:00';
$iconic_wds->settings['datesettings_datesettings_nextday_cutoff'] = false; // no cutoff.
}
}
add_action( 'init', 'iconic_change_same_next_day_cutoff', 11 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment