gpt4 book ai didi

php - Woocommerce 获取当前的运输区域

转载 作者:可可西里 更新时间:2023-10-31 22:40:30 25 4
gpt4 key购买 nike

我创建了自定义送货方式。( https://docs.woocommerce.com/document/shipping-method-api/ )

在 calculate_shipping 函数中,我想根据送货区域设置送货方式的价格:

如果运输区域是“区域 1”,则将价格设置为 15否则将价格设置为 20

所以我想知道,我怎样才能得到当前的运输区域?

我已经尝试查看此文档:https://docs.woocommerce.com/wc-apidocs/class-WC_Shipping_Zone.html可能我没看懂...

public function calculate_shipping( $package=Array()) {
global $woocommerce;

$cart_total=$woocommerce->cart->cart_contents_total;


$current_hour=date('His');
$start_hour='110000';
$end_hour='210000';

// CONDITIONAL CHECK

// OPEN HOUR
if($current_hour >= $start_hour && $current_hour <= $end_hour){
$is_open=true;
}else{
$is_open=false;
}

// price PER ZONE
$zone=""; // need to know how to get zone name or zone id
if($zone=='Zone 1' && $cart_total>='60'){
$min_order=true;
$cost = 6;
}elseif($zone=='Zone 2' && $cart_total>='120'){
$min_order=true;
$cost = 8;
}elseif($zone=='Zone 3' && $cart_total>='180'){
$min_order=true;
$cost = 9;
}else{
$min_order=false;
$cost = 0;
}

if($is_open==true && $min_order==true){
$allowed=true;
}else{
$allowed=false;
}


$rate = array(
'id' => $this->id,
'label' => 'Livraison Express T '.wc_get_shipping_zone().' T',
'cost' => $cost,
);

// Register the rate
if($allowed==true){
$this->add_rate( $rate );
}
}

最佳答案

我自己想办法

像那样:

$shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package );

$zone=$shipping_zone->get_zone_name();

完整功能:

public function calculate_shipping( $package=Array()) {
global $woocommerce;

$shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package );

$zone=$shipping_zone->get_zone_name();

if($zone=='Zone 1'){
$cost = 6;
}else{
$cost=12;
}
$rate = array(
'id' => $this->id,
'label' => 'Delivery Name',
'cost' => $cost,
);

$this->add_rate( $rate );

}

关于php - Woocommerce 获取当前的运输区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43916432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com