-->

Display an estimated delivery date range based on

2020-02-29 10:51发布

问题:

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.

This is what I have written so far. It goes in the function.php

function lieferzeit() {

    global $woocommerce;
        $cart_items = $woocommerce->cart->get_cart();
        foreach ($cart_items as $variation) {
            $variation_id = $variation['variation_id'];
         $variation_obj = new WC_Product_variation($variation_id);
         $stock = $variation_obj->get_stock_quantity();
        }
echo $stock; //outputs the in stock amount successfully

}


add_filter ( 'woocommerce_cart_collaterals', 'lieferzeit');

Now I am trying to add the estimated date but here I am stuck

function lieferzeit() {

    global $woocommerce;
        $cart_items = $woocommerce->cart->get_cart();
        foreach ($cart_items as $variation) {
            $variation_id = $variation['variation_id'];
         $variation_obj = new WC_Product_variation($variation_id);
         $stock = $variation_obj->get_stock_quantity();
        }

        for ($i=0; $i < count($stock) ; $i++) {
            echo "Voraussichtliche Lieferung Date! ";


        }
}

add_filter ( 'woocommerce_cart_collaterals', 'lieferzeit');

Here the date output have to be defined. From today +1 day until from today +4 days. But I don't have an idea how to manage that. The best output would be this format:

Estimated shipping Fri. 14.7 - Wed. 19.7

I am not even shure if

for ($i=0; $i < count($stock) ; $i++) {

is the right way to go.

I do have two types of products, one can be shipped within 1-4 days and the other is within 14-21 days. Now is second problem. When both types are in the cart the higher shipping time should be selected.

Are there some ideas?


Update:

The code should check the stock quantity of each item in the cart. If all items have a stock quantity greater than 0 it should echo an estimated shipping time of 1 to 4 working days given as a date.

If there is one item in cart with a stock quantity of 0 or below it should echo an estimate shipping time of 14 - 21 working days given as a date. Even if all the other items in the cart have a stock quantity greater than 0.

Working days should be monday until friday. Very neat would be if the code recognizes holidays too e.g. christmas, new year etc..

Thanks

The solution from LoicTheAztec works perfect. Now I tried to add some more option to it.

It would be nice if the output of function lieferzeit() would be displayed in the admin order details page. To create a custom admin panel in the sidebar I found

    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 $lieferzeit;
}

from this post

This works so far and there is a Order Custom tab in the admin page. Now I tried to store the output of function lieferzeit() in a variable.

$from = str_replace($days_en, $days_ge, $from);
$to   = str_replace($days_en, $days_ge, $to);

$lieferzeit = array($from, $to);

But it seems that function add_meta_boxes() and function order_my_custom() doen't know nothing of the variable $lieferzeit.

Is there another way to store and recall the output of function lieferzeit() ?

回答1:

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