3

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 file

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 sure 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() ?

starball
  • 20,030
  • 7
  • 43
  • 238
Max
  • 173
  • 5
  • 18
  • I have seen your question about the products only yet. Stackoverflow is a little bit chaotic :) I use both single and variable products. But your solution is working very good for both types! – Max Jul 12 '17 at 16:08
  • **I have updated my code** regarding simple and variable product stock… **It should work now** …*You could also use stock status `if($product_obj->get_stock_status() == 'instock'){` or `if($product_obj->get_stock_status() == 'outofstock'){`* … I hope this time is OK – LoicTheAztec Jul 13 '17 at 00:07

1 Answers1

3

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi LoicTheAztec, thank you so much for your help!! Thats unbelievable. I don't know how many days I would have spent on this without your help. The only thing which seems more complicated that I thought it would be, is to get an output in German. I like this format `('D, j.n.', strtotime("+$start days"))` a lot. But a simple add of `setlocale(LC_TIME, "de_DE");` won't work. And replace all the strtotime with strftime doesn't work too. Even with changing the dateformat to `%a, %d.%m.` – Max Jul 12 '17 at 15:22
  • There is something I have discovered only now. I thought it works with single products. It does only when the single product is out of stock. For a single product which is in stock and has the short delivery time it only shows the long shipping time. I tried to add this to the beginning, but it didn't help. `$simple_id = $cart_item['simple_id']; $simple_obj = new WC_Product_simple($simple_id); $stock_simple = $simple_obj->get_stock_quantity();` And then I added fellowing. `if( $variation_obj->get_stock_quantity() <= 0 xor $simple_obj->get_stock_quantity() <= 0){` – Max Jul 12 '17 at 22:41
  • Its working perfectly now! I've made a little change, so that products with less than 0 stock quantity are in the condition too. I changed `if( 0 == $stock )` to `if( $stock <= 0 )` – Max Jul 13 '17 at 08:18