php中根据商品规格查库存,php-根据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.

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

谢谢

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

如果函数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;

}

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

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

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

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

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

还有另一种方法来存储和调用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 "
Estimated shipping $from - $to";

}

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

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

标签:cart,woocommerce,variations,wordpress,php

来源: https://codeday.me/bug/20191111/2018036.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值