如何在woocommerce中添加自定义运费? [英] How to add custom shipping charge in woocommerce?

查看:391
本文介绍了如何在woocommerce中添加自定义运费?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用woocommerce中的代码添加运费.这是我的要求.

I want to add shipping charge using code in woocommerce. here is my reuirements.

如果我的运输国家是澳大利亚,则运输费用会有所不同,而在澳大利亚境外也将有所不同. 现在,如果我的运送国家是澳大利亚和

If my shipping country is Australia then shipping charge is different and outside australia is also different. now, if my shipping country is Australia and

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

如果我的运输国家/地区不在澳大利亚和

If my shipping country is outside Australia and

 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

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

So, how can I add custom shipping charge as per my above requirements when user change shipping country from checkout page. I tried below code but it only works on order value, how can I add shipping country in below code in custom plugin.

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'类,然后使功能如下:

Better to make custom plugin for shipping charge where you can use hook.
First extend 'WC_Your_Shipping_Method' class in your custom plugin and make function like this:

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',
        'cost' => $cost,
        'calc_tax' => 'per_order'
    );

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

这篇关于如何在woocommerce中添加自定义运费?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆