加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何在woocommerce中添加自定义运费?

发布时间:2020-12-13 13:12:17 所属栏目:PHP教程 来源:网络整理
导读:我想使用woocommerce中的代码添加运费.这是我的要求. 如果我的运输国家是澳大利亚,则运费不同,澳大利亚境外也不同. 现在,如果我的运输国家是澳大利亚和 1. if order value is 100,then shipping charge is $100 2. if order value is 100,then shipping char
我想使用woocommerce中的代码添加运费.这是我的要求.

如果我的运输国家是澳大利亚,则运费不同,澳大利亚境外也不同.
现在,如果我的运输国家是澳大利亚和

1. if order value is < 100,then shipping charge is $100 
2. if order value is > 100,then shipping charge is $0.

如果我的运输国家在澳大利亚境外

1. if order value is < 500,then shipping charge is $60
 2. if order value is > 500 and < 1000,then shipping charge is $50
 3. if order value is > 1000,then shipping charge is $0

那么,当用户从结帐页面更改送货国家时,如何根据我的上述要求添加自定义运费.
我尝试下面的代码,但它只适用于订单价值,如何在自定义插件中的下面的代码中添加运输国家/地区.

class WC_Your_Shipping_Method extends WC_Shipping_Method {
    public function calculate_shipping( $package ) {
    global $woocommerce;
        if($woocommerce->cart->subtotal > 5000) {
            $cost = 30; 
        }else{
            $cost = 3000;
      }
}
$rate = array(
    'id' => $this->id,'label' => $this->title,'cost' => $cost,'calc_tax' => 'per_order'
);

// Register the rate
$this->add_rate( $rate );

}

最好为可以使用钩子的运费制作自定义插件.
首先在自定义插件中扩展’WC_Your_Shipping_Method’类,并使这样的函数:
public function calculate_shipping( $package ) {
    session_start();
    global $woocommerce;

    $carttotal = $woocommerce->cart->subtotal;
    $country = $_POST['s_country']; //$package['destination']['country'];

    if($country == 'AU')
    {
        if($carttotal > 100){
            $cost = 5;
        }else{
            $cost = 10;//10.00;
        }
    }
    else
    {
        if($carttotal < 500){
            $cost = 60;//60.00;
        }else if($carttotal >= 500 && $carttotal <= 1000){
            $cost = 50;//50.00;
        }else if($carttotal > 1000){
            $cost = 0;
        }
    }

    $rate = array(
        'id' => $this->id,'label' => 'Shipping','calc_tax' => 'per_order'
    );

    // Register the rate
    $this->add_rate( $rate );
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读