Woocommerce获得当前的运输区域

时间:2017-05-11 13:09:28

标签: php wordpress woocommerce shipping

我创建了一个自定义送货方式。 (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 );
            }
        }

1 个答案:

答案 0 :(得分:5)

我自己找到了解决方案

就像那样:

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

}