【问题标题】:Woocommerce get current shipping zoneWoocommerce 获取当前运输区域
【发布时间】:2023-03-09 10:34:01
【问题描述】:

我创建了一种自定义运输方式。 (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 );
            }
        }

【问题讨论】:

    标签: php wordpress woocommerce shipping


    【解决方案1】:

    我自己找到解决办法

    这样:

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

    【讨论】:

    猜你喜欢
    • 2018-05-05
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多