根据WooCommerce购物车项目库存显示估计的交货日期范围

时间:2017-07-12 07:29:13

标签: php wordpress woocommerce cart variations

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

我有点成功,但现在我被卡住了。

这是我到目前为止所写的内容。它进入了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;
}

from this post

这项工作到目前为止,管理页面中有一个订单自定义选项卡。现在我尝试将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()输出的方法吗?

1 个答案:

答案 0 :(得分:3)

更新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文件中,或者放在任何插件文件中。

此代码经过测试并正常运行