Last active
January 21, 2019 05:52
-
-
Save renjith-ph/7ea21e6c60af0bab989ed2a57e98477e to your computer and use it in GitHub Desktop.
Snippet to change estimate delivery if product is in back order.
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
/** | |
* Snippet to change estimate delivery if product is in back order. | |
* Created at : 08 Dec 2018 | |
* PluginHive Plugins : https://www.pluginhive.com/plugins/ | |
* Gist Link : https://gist.github.com/renjith-ph/7ea21e6c60af0bab989ed2a57e98477e | |
*/ | |
add_filter( 'woocommerce_get_availability', 'woocommerce_get_availability',12,2 ); | |
function woocommerce_get_availability( $stock_arr ,$item) { | |
if( empty($item) ){ | |
global $product; | |
if( ! is_object($product) ) { | |
global $post; | |
$product = wc_get_product($post->ID); | |
} | |
$item = $product; | |
} | |
$date_format='d/m/Y'; // should same as date format in plugin settings | |
$available=array( | |
'370' => '16/01/2019', // product_id => no of days | |
'61' => '20/12/2018' | |
); | |
$product_id=(wp_get_post_parent_id($item->get_id())==0) ? $item->get_id() : wp_get_post_parent_id($item->get_id()); | |
if(!empty($stock_arr) && !empty($stock_arr['availability']) && $item->get_stock_status()=='onbackorder' ) | |
{ | |
$display_text="Estimated delivery by"; // should be same as Product Page Text in plugin settings | |
$text_arr=explode($display_text, $stock_arr['availability']); | |
$availability=$stock_arr['availability']; | |
if(isset($text_arr[1])) | |
{ | |
$product_page_sample_text = get_option('wf_estimated_delivery_product_page_text_simple'); | |
$product_page_sample_text = !empty( $product_page_sample_text ) ? $product_page_sample_text : '<p style="color:green;font-size:small">'.__( 'Estimated delivery by', 'wf_estimated_delivery' ).' [date]</p>'; | |
$product_page_text_range = get_option('wf_estimated_delivery_product_page_text_range'); | |
$product_page_text_range = !empty( $product_page_text_range ) ? $product_page_text_range : '<p style="color:green;font-size:small">'.__( 'Estimated delivery by','wf_estimated_delivery' ).' [date_1] - [date_2]</p>'; | |
$product_page_sample_text = apply_filters( 'wpml_translate_single_string', $product_page_sample_text, 'wf_estimated_delivery', 'product_page_simple_text'); | |
$product_page_text_range = apply_filters( 'wpml_translate_single_string', $product_page_text_range, 'wf_estimated_delivery', 'product_page_range_text'); | |
if(!isset($available[$product_id])) | |
{ | |
return $stock_arr; | |
} | |
$available_within=date_create_from_format('d/m/Y',$available[$product_id]); | |
$current_time = current_time('Y m d H:i:s'); | |
$today_wp_date = date_create_from_format( 'Y m d H:i:s', $current_time); | |
$today_wp_date_seconds = $today_wp_date->format('U'); | |
$available_within = ceil( ($available_within->format('U') - $today_wp_date_seconds ) / 86400 ); | |
$date_array=explode('</p>', $text_arr[1]); | |
$date_arr=explode('-', $date_array[0]); | |
if(!isset($date_arr[1])) // for simple range | |
{ | |
$date= date_create_from_format( $date_format ,trim($date_arr[0])); | |
if( is_a($date, 'DateTime') ) { | |
$date->modify("+$available_within days"); | |
$true=1; | |
$wf_workdays=get_option( 'wf_estimated_delivery_operation_days' ); | |
while($true==1) | |
{ | |
if(in_array( strtolower(date_format($date,'D')), $wf_workdays )) | |
{ | |
$true=0; | |
} | |
else | |
{ | |
$date->modify( "1 day" ); | |
} | |
} | |
$date=$date->format($date_format); | |
$availability = str_replace('[date]', $date , $product_page_sample_text ); | |
$old_text=str_replace('[date]', trim($date_arr[0]) , $product_page_sample_text ); | |
} | |
} | |
$old_availability=explode($old_text,$stock_arr['availability']); | |
$stock_arr['availability']=$old_availability[0].$availability; | |
} | |
} | |
return $stock_arr; | |
} | |
add_filter( 'xa_estimated_delivery_cart_checkout_page_html_formatted_date', 'xa_estimated_delivery_cart_checkout_page_html_formatted_date',10,2 ); | |
function xa_estimated_delivery_cart_checkout_page_html_formatted_date( $text, $test ) { | |
global $woocommerce; | |
$date_format='d/m/Y'; // should same as date format in plugin settings | |
$available=array( | |
'370' => '16/01/2019', // product_id => no of days | |
'61' => '20/12/2018' | |
); | |
$items = $woocommerce->cart->get_cart(); | |
$max_count=0; | |
foreach($items as $item => $values) { | |
$product_id=(wp_get_post_parent_id($values['data']->get_id())==0) ? $values['data']->get_id() : wp_get_post_parent_id($values['data']->get_id()); | |
if(isset($available[$product_id]) && $values['data']->get_stock_status()=='onbackorder' ) | |
{ | |
$available_within=date_create_from_format('d/m/Y',$available[$product_id]); | |
$current_time = current_time('Y m d H:i:s'); | |
$today_wp_date = date_create_from_format( 'Y m d H:i:s', $current_time); | |
$today_wp_date_seconds = $today_wp_date->format('U'); | |
$available_within = ceil( ($available_within->format('U') - $today_wp_date_seconds ) / 86400 ); | |
if($max_count<$available_within) | |
{ | |
$max_count=$available_within; | |
} | |
} | |
} | |
$display_text = "Estimated Delivery"; // Text for Estimated delivery | |
$text_array = explode( '<td data-title="'.$display_text.'" >', $text ); //cart | |
if(!isset($text_array[1])) // check text format is simple | |
{ | |
$text_array = explode( '<td data-title="'.$display_text.'">', $text ); //checkout | |
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(!isset($prev_date_arr[1])) { | |
$max_date = date_create_from_format( $date_format, trim($prev_date_arr[0]) ); | |
$max_date->modify( "$max_count day" ); | |
$true=1; | |
$wf_workdays=get_option( 'wf_estimated_delivery_operation_days' ); | |
while($true==1) | |
{ | |
if(in_array( strtolower(date_format($max_date,'D')), $wf_workdays )) | |
{ | |
$true=0; | |
} | |
else | |
{ | |
$max_date->modify( "1 day" ); | |
} | |
} | |
$text = '<tr class="shipping"><th> '.$display_text.'</th><td data-title="'.$display_text.'"> '.$max_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_estimated_delivery_thank_you_page_html_formatted_date', 10, 2 ); | |
function xa_estimated_delivery_thank_you_page_html_formatted_date( $text, $test ) { | |
global $woocommerce; | |
$date_format='d/m/Y'; // should same as date format in plugin settings | |
$available=array( | |
'370' => '16/01/2019', // product_id => no of days | |
'61' => '20/12/2018' | |
); | |
$items = $woocommerce->cart->get_cart(); | |
$max_count=0; | |
foreach($items as $item => $values) { | |
$product_id=(wp_get_post_parent_id($values['data']->get_id())==0) ? $values['data']->get_id() : wp_get_post_parent_id($values['data']->get_id()); | |
if(isset($available[$product_id]) && $values['data']->get_stock_status()=='onbackorder' ) | |
{ | |
$available_within=date_create_from_format('d/m/Y',$available[$product_id]); | |
$current_time = current_time('Y m d H:i:s'); | |
$today_wp_date = date_create_from_format( 'Y m d H:i:s', $current_time); | |
$today_wp_date_seconds = $today_wp_date->format('U'); | |
$available_within = ceil( ($available_within->format('U') - $today_wp_date_seconds ) / 86400 ); | |
if($max_count<$available_within) | |
{ | |
$max_count=$available_within; | |
} | |
} | |
} | |
$display_text = "Estimated Delivery"; // Text for Estimated delivery | |
$text_array = explode( '-', trim( $text) ); | |
if(!isset($text_array[1])) { | |
$max_date = date_create_from_format( $date_format, trim($text_array[0]) ); | |
$max_date->modify( "$max_count day" ); | |
$true=1; | |
$wf_workdays=get_option( 'wf_estimated_delivery_operation_days' ); | |
while($true==1) | |
{ | |
if(in_array( strtolower(date_format($max_date,'D')), $wf_workdays )) | |
{ | |
$true=0; | |
} | |
else | |
{ | |
$max_date->modify( "1 day" ); | |
} | |
} | |
$text = $max_date->format($date_format); | |
} | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment