根据WooCommerce购物车项目库存显示估计的交货日期范围 [英] Display an estimated delivery date range based on WooCommerce cart item stock

查看:83
本文介绍了根据WooCommerce购物车项目库存显示估计的交货日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据购物车中产品的库存状态在购物车中输出预计的交货日期.

我取得了一些成功,但现在我被困住了.

这是我到目前为止所写的.它放在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');

现在我想添加估计的日期,但是在这里我被卡住了

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

这里必须定义日期输出.从今天+1天到今天+4天.但是我不知道该如何管理. 最好的输出将是以下格式:

预计星期五发货. 14.7-星期三. 19.7

我什至都不愿意

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

是正确的方法.

我确实有两种类型的产品,一种可以在1-4天内发货,另一种可以在14-21天内发货.现在是第二个问题.当两种类型都在购物车中时,应选择更长的运输时间.

有什么想法吗?


更新:

代码应检查购物车中每件商品的库存数量. 如果所有物品的库存数量均大于0,则应与日期对应的预计运输时间为1-4个工作日.

如果购物车中有一件库存数量为0或以下的物品,则应附上预计的14-21个工作日的交货日期作为日期.即使购物车中的所有其他物品的库存数量都大于0.

工作日应为星期一至星期五.如果代码也可以识别假期,例如,代码会非常整洁.圣诞节,新年等.

谢谢

LoicTheAztec的解决方案非常完美.现在,我尝试向其中添加更多选项.

如果function lieferzeit()的输出将显示在管理订单详细信息页面中,那就太好了. 要在我发现的侧边栏中创建自定义管理面板

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

来自此帖子

到目前为止,此方法有效,并且管理页面中有一个订单自定义"标签.现在,我尝试将function lieferzeit()的输出存储在变量中.

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

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

但是function add_meta_boxes()function order_my_custom()似乎对变量$lieferzeit一无所知.

还有另一种方法来存储和调用function lieferzeit()的输出吗?

解决方案

更新4 (于2018年9月发布)

下面的代码将检查购物车中每件商品的库存数量.

1)如果所有购物车物品都有货",它将回显作为日期给出的1-4个工作日的预计运送时间.

2)如果一个购物车产品缺货",它将回显作为日期给出的14到21个工作日的预计运输时间.

但我不会识别假期

这是代码:

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";
}

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

此代码已经过测试并且可以正常工作

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

解决方案

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

这篇关于根据WooCommerce购物车项目库存显示估计的交货日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆