Skip to content

Instantly share code, notes, and snippets.

@renjith-ph
Last active January 4, 2019 12:37
Show Gist options
  • Save renjith-ph/2dd62f1cbba26a4656379bb2f033d7ff to your computer and use it in GitHub Desktop.
Save renjith-ph/2dd62f1cbba26a4656379bb2f033d7ff to your computer and use it in GitHub Desktop.
Snippet to adjust Date Range Estimated delivery based on shipping method.PluginHive Plugins : https://www.pluginhive.com/plugins/
/**
* Snippet to adjust Date Range Estimated delivery based on shipping method.
* Created at : 28 Nov 2018
* Updated on : 04 Jan 2019
* PluginHive Plugins : https://www.pluginhive.com/plugins/
* Gist Link : https://gist.github.com/xadapter/983ecf750115b2c0471f55a35a57f8ef
*/
// Adjustment on Cart and Checkout page.
add_filter( 'xa_estimated_delivery_cart_checkout_page_html_formatted_date', 'xa_modify_cart_page_text', 10, 2 );
function xa_modify_cart_page_text( $text, $test ) {
$shipping_method=WC()->session->get( 'chosen_shipping_methods' );
if(!empty($shipping_method))
{
$shipping_method=strtolower($shipping_method[0]);
}
$display_text = "Estimated Delivery"; // Text for Estimated delivery
$date_format = 'd/m/Y'; // Date Format
$adjustment = array(
'table_rate:2:1' => array( // shipping method name
'min' => '-2', // Date Lower range
'max' => 6, // Date Higher range
),
);
$text_array = explode( '<td data-title="'.$display_text.'">', $text );
if(!isset($text_array[1])) // check text format is simple
{
return $text;
}
$text_array = explode( '</td></tr>', $text_array[1] );
$prev_date_arr = explode( '-', $text_array[0] ); //Contains the date in case of date range
if(!empty($shipping_method) && isset($adjustment[$shipping_method]) && isset($prev_date_arr[1]) && !strpos($prev_date_arr[1], 'days') ) {
$lower_range_date = date_create_from_format( $date_format, trim($prev_date_arr[0]) );
$higher_range_date = date_create_from_format( $date_format, trim($prev_date_arr[1]) );
$min = $adjustment[$shipping_method]['min'];
$max = $adjustment[$shipping_method]['max'];
$lower_range_date->modify( "$min day" );
$higher_range_date->modify( "$max day" );
$text = '<tr class="shipping"><th> '.$display_text.'</th><td data-title="'.$display_text.'"> '.$lower_range_date->format($date_format) .' - '. $higher_range_date->format($date_format).'</td></tr>';
}
return $text;
}
// Adjustment on Thank you page.
add_filter( 'xa_estimated_delivery_thank_you_page_html_formatted_date', 'xa_modify_thankyou_page_text', 10, 2 );
function xa_modify_thankyou_page_text( $text, $test ) {
$shipping_method=WC()->session->get( 'chosen_shipping_methods' );
if(!empty($shipping_method))
{
$shipping_method=strtolower($shipping_method[0]);
}
$display_text = "Estimated Delivery"; // Text for Estimated delivery
$date_format = 'd/m/Y'; // Date Format
$adjustment = array(
'table_rate:2:1' => array( // shipping method name
'min' => 5, // Date Lower range
'max' => 6, // Date Higher range
),
);
$prev_date_arr = explode( '-', $text ); // Date as array
if(!empty($shipping_method) && isset($adjustment[$shipping_method]) && isset($prev_date_arr[1]) && !strpos($prev_date_arr[1], 'days') ) {
$lower_range_date = date_create_from_format( $date_format, trim($prev_date_arr[0]) );
$higher_range_date = date_create_from_format( $date_format, trim($prev_date_arr[1]) );
$min = $adjustment[$shipping_method]['min'];
$max = $adjustment[$shipping_method]['max'];
$lower_range_date->modify( "$min day" );
$higher_range_date->modify( "$max day" );
$text = $lower_range_date->format($date_format).' - '.$higher_range_date->format($date_format);
}
return $text;
}
add_filter( 'woocommerce_package_rates', 'add_est_delivery_to_shipping_rates', 11, 2 );
function add_est_delivery_to_shipping_rates($shipping_rates, $package ) {
$adjustment = array(
'flat_rate:4' => array( // shipping method name
'min' => '-1', // Date Lower range
'max' => 6, // Date Higher range
),
'free_shipping:5' => array(
'min' =>1,
'max' =>1),
);
$date_format = 'd/m/Y'; // Date Format
foreach ($shipping_rates as $shipping_method => $shipping_rate_data) {
$method_meta_data_arr = $shipping_rate_data->get_meta_data();
if(array_key_exists($shipping_method, $adjustment) && isset($method_meta_data_arr['ph_est_delivery_date']))
{
$package_est_delivery=$method_meta_data_arr['ph_est_delivery_date'];
$prev_date_arr=explode('-', $package_est_delivery);
if(!isset($prev_date_arr[1]))
{
continue;
}
elseif(strpos($prev_date_arr[1], 'days'))
{
$lower_range_date=trim($prev_date_arr[0])+$adjustment[$shipping_method]['min'];
$higher_range_date=trim(explode('days', $prev_date_arr[1])[0])+$adjustment[$shipping_method]['max'];
$estimated_delivery_date=$lower_range_date.' - '.$higher_range_date.' days';
}
else
{
$lower_range_date = date_create_from_format( $date_format, trim($prev_date_arr[0]) );
$higher_range_date = date_create_from_format( $date_format, trim($prev_date_arr[1]) );
$min = $adjustment[$shipping_method]['min'];
$max = $adjustment[$shipping_method]['max'];
$lower_range_date->modify( "$min day" );
$higher_range_date->modify( "$max day" );
$estimated_delivery_date=$lower_range_date->format($date_format) .' - '. $higher_range_date->format($date_format);
}
$shipping_rate_data->add_meta_data( 'ph_est_delivery_date', $estimated_delivery_date );
}
}
return $shipping_rates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment