Skip to content

Instantly share code, notes, and snippets.

@varun-pluginhive
Created September 17, 2018 14:08
Show Gist options
  • Save varun-pluginhive/1bdeb6c3b18a126cdae99ab92b451892 to your computer and use it in GitHub Desktop.
Save varun-pluginhive/1bdeb6c3b18a126cdae99ab92b451892 to your computer and use it in GitHub Desktop.
Snippet to change Estimated delivery text based on stock quantity. ESTIMATED DELIVERY DATE PLUGIN FOR WOOCOMMERCE - https://www.pluginhive.com/product/estimated-delivery-date-plugin-woocommerce/
/**
* Snippet to change Estimated delivery text based on stock quantity.
* Created at : 17 Sep 2018
* Updated at : 17 Sep 2018
* PluginHive Plugins : https://www.pluginhive.com/plugins/
* Gist Link : https://gist.github.com/varun-pluginhive/1bdeb6c3b18a126cdae99ab92b451892
*/
if( ! function_exists('change_estimated_delivery_text_on_product_page') ) {
function change_estimated_delivery_text_on_product_page( $stock_arr, $item ) {
$product_page_text_in_settings = "Estimated delivery by [date_1] - [date_2] days";
$text_before_range = "Estimated delivery by ";
$text_after_range = "days";
$quantity = 0; // Quantity
$text_less_than_quantity = "Estimated delivery [date_1] - [date_2] days"; // Text for less than $quantity
$text_more_than_quantity = "delivery by [date_1] - [date_2] days"; // Text for more than $quantity
if( is_admin() ) return;
$stock_quantity = $item->get_stock_quantity();
$date_range = explode( $text_before_range, $stock_arr['availability']);
$other_text = $date_range[0];
if( ! empty($date_range[1]) ) {
$date_range = explode( $text_after_range, $date_range[1]);
$date_range = trim($date_range[0]);
if( $stock_quantity >= $quantity ) {
$new_text = str_replace( "[date_1] - [date_2]", $date_range, $text_more_than_quantity);
}
else{
$new_text = str_replace( "[date_1] - [date_2]", $date_range, $text_less_than_quantity);
}
}
if( ! empty($new_text) ) {
$stock_arr['availability'] = $other_text.$new_text;
}
return $stock_arr;
}
add_filter('woocommerce_get_availability', 'change_estimated_delivery_text_on_product_page', 12, 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment