Display an estimated delivery date range based on stock in your WooCommerce cart

I am trying to get an estimated cart delivery date based on the stock status of products in the cart.

I was a bit successful, but now I'm stuck.

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

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 a calculated 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');

      

Date output must be defined here. From today +1 day to today +4 days. But I have no idea how this can be done. The best result will be this format:

Estimated Delivery Fri. 14.7 - cf. 19.7

I don't even understand if

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

      

- the right way.

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

Any 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 must repeat the estimated delivery time of 1 to 4 business days indicated as the date.

If there is one item in the cart with a stock quantity of 0 or less, it must repeat the evaluation delivery time within 14 to 21 business days indicated as the date. Even if all other items in the basket have a stock quantity greater than 0.

Business days must be Monday through Friday. Very neat if the code recognizes holidays, for example. christmas, new year, etc.

thank

LoicTheAztec's solution works great. Now I have tried to add another option.

It would be nice if the output function lieferzeit()

is displayed on the admin detail 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 an Order to Order tab on the admin page. Now I tried to store the output 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()

do not know anything about the variable $lieferzeit

.

Is there any other way to save and call the output function lieferzeit()

?

+3


source to share


1 answer


Update 4 (September 2018)

The code below will check the stock quantity of each item in the cart.

1) If all items in your cart are "in stock", the delivery time will be 1 to 4 business days.

2) If one basket of an item is "out of stock", this will mean an estimated delivery time of 14 to 21 business days indicated as the date.

But I won't acknowledge the holidays



Here's the 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";
}

      

The code is placed in the function.php file of your active child theme (or theme) or also in any plugin file.

This code has been tested and works

+3


source







All Articles