【问题标题】:How to add custom shipping charge in woocommerce?如何在 woocommerce 中添加自定义运费?
【发布时间】:2014-12-27 10:47:36
【问题描述】:

我想在 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 );

}

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    最好制作自定义插件来收取运费,您可以在其中使用挂钩。
    首先在您的自定义插件中扩展“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',
            'cost' => $cost,
            'calc_tax' => 'per_order'
        );
    
        // Register the rate
        $this->add_rate( $rate );
    }
    

    【讨论】:

    • 嘿,它的工作.. 非常感谢这段代码。我从上面的函数中得到了我的解决方案
    【解决方案2】:

    首先将管理员名称中的送货方式设置为“myship”

    然后在你的主题functions.php文件中添加以下代码

    add_action('woocommerce_before_cart_table', 'discount_when_produts_in_cart');
    
    function discount_when_produts_in_cart( ) {
    
        global $woocommerce;
    
     $coupon_code = 'myship';
    
        if( $woocommerce->cart->get_cart_total() > 500 ) {
    
            $coupon_code = 'myship';
    
       }
    
     else
    
     {
    
       $woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code));
    
        $woocommerce->clear_messages();
    
     }
    

    【讨论】:

    • 嗨,我知道你为什么要添加优惠券代码?以及如何知道上面代码中选择了哪个送货国家?
    • 这并不能真正回答 OP 的问题。
    猜你喜欢
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 2021-08-06
    • 2018-11-15
    • 2017-07-09
    • 1970-01-01
    • 2021-04-24
    • 2017-12-23
    相关资源
    最近更新 更多