I am trying to output an estimated delivery date in the cart based on the stock status of the products in the cart.
I was a little bit successful but now I am stuck
Update 4 (on September 2018)
The code below, will check the stock quantity of each item in the cart.
1) If all cart items are "in stock" it will echo an estimated shipping time of 1 to 4 working days given as a date.
2) If one cart items is "out of stock" it will echo an estimated shipping time of 14 to 21 working days given as a date.
But I will not recognize holidays
Here is that code:
add_filter ( 'woocommerce_cart_collaterals', 'lieferzeit');
function lieferzeit() {
$all_items_in_stock = true; // initializing
// Iterating through cart items (to get the stock info)
foreach (WC()->cart->get_cart() as $cart_item) {
// The cart item stock quantity
$stock = $cart_item['data']->get_stock_quantity();
if( $stock <= 0 ){
// if an item is out of stock
$all_items_in_stock = false;
break; // We break the loop
}
}
// Items "in stock" (1 to 4 week days)
if( $all_items_in_stock ){
for( $start=0, $count=-1 ; $count < 4; $start++ ){
$weekdays = date('w', strtotime("+$start days"));
if( $weekdays > 0 && $weekdays < 6 ){
$count++;
echo date('D j (w)', strtotime("+$start days")).', ';
if($count == 1){
$from = date('D. j/n', strtotime("+$start days") );
} elseif($count == 4) {
$to = date('D. j/n', strtotime("+$start days") );
}
}
}
} else { // 1 is Items Out of stock (14 to 21 week days)
for( $start=0, $count=-1 ; $count < 21; $start++ ){
$weekdays = date('w', strtotime("+$start days"));
if( $weekdays > 0 && $weekdays < 6 ){
$count++;
if($count == 14){
$from = date('D. j/n', strtotime("+$start days") );
} elseif($count == 21) {
$to = date('D. j/n', strtotime("+$start days") );
}
}
}
}
## TRANSLATION ##
// DAYS IN ENGLISH (Source)
$days_en = array('Mon','Tue','Wed','Thu','Fri');
// TRANSLATE the DAYS in GERMAN (replacement)
$days_ge = array('Mmm','Ttt','Www','Thh','Fff');
$from = str_replace( $days_en, $days_ge, $from );
$to = str_replace( $days_en, $days_ge, $to );
## OUTPUT ##
echo "<br><br>Estimated shipping $from - $to";
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works