I use woocommerce and have written a function, which displays a date range for expected delivery based on the stock status...
For reference: Display an estimated delivery date range based on WooCommerce cart item stock
Now I want to output the calculated date range into my admin panel for each order.
For the Orders edit pages in the Wordpress backend I use this code:
add_action( 'add_meta_boxes', 'add_meta_boxes' );
function add_meta_boxes() {
add_meta_box( 'woocommerce-order-my-custom', __( 'Order Custom' ),
'order_my_custom', 'shop_order', 'side', 'default'
);
}
function order_my_custom(){
echo "Voraussichtliche Lieferung<br> $from - $to";
}
The date range is calculated and displayed on front end with this code:
add_filter ( 'woocommerce_cart_collaterals', 'lieferzeit');
add_filter ( 'woocommerce_thankyou_lieferung', '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) {
# HANDLING SIMPLE AND VARIABLE PRODUCTS
// Variable products
$variation_id = $cart_item['variation_id'];
if( 0 != $variation_id) {
$variation_obj = new WC_Product_variation($variation_id);
$stock = $variation_obj->get_stock_quantity();
} else {
// Simple products
$product_id = $cart_item['product_id'];
$product_obj = new WC_Product($product_id);
$stock = $product_obj->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, d.m.', strtotime("+$start days") );
} elseif($count == 4) {
$to = date('D, d.m.', 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, d.m.', strtotime("+$start days") );
} elseif($count == 21) {
$to = date('D, d.m.', 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('Mo','Di','Mi','Do','Fr');
$from = str_replace($days_en, $days_ge, $from);
$to = str_replace($days_en, $days_ge, $to);
## OUTPUT ##
// echo "Voraussichtliche Lieferung<br> $from - $to";
echo '<i class="shipping_icon fa fa-truck fa-flip-horizontal" aria-hidden="true"></i> <div class="lieferung"> Vorauslichtliche Lieferung </div> <div class="fromto_date"> ' . $from . ' – ' . $to . ' </div> <div class="tooltip">
<span class="tooltiptext">Gilt nur bei Lieferungen nach Deutschland.</span></div>' ;
}
My problem is that the "estimated delivery range" data of function lieferzeit()
don't appear in function add_meta_boxes()
.
It would like to store the "estimated delivery range" data (of function lieferzeit()
) when order is submitted to display it in the corresponding order backend metabox.
If I try to add the output of function lieferzeit()
in the thank you page it works, but it doesn't store the data.
So how to save and get the calculated date rage data to use it everywhere I need?
Thanks