【问题标题】:Shipping cost based on cart total weight in Woocommerce 3Woocommerce 3 中基于购物车总重量的运费
【发布时间】:2019-01-23 15:37:09
【问题描述】:

在我的 Woocommerce 网上商店中,我确实有不同的产品。我想根据购物车物品的总重量计算运费:

  • 从0 到6 公斤的成本是5 €,
  • 从6 到12 公斤成本是9 €

实际上,如果我有一个 1 公斤的产品,运费是 5 €,但如果我的购物篮中有 10 件该产品,运费费用仍然是 5 € (应该改为 9 €)。

如何根据购物车总重量计算累进运费?

【问题讨论】:

    标签: php wordpress woocommerce fee shipping-method


    【解决方案1】:

    尝试以下功能,该功能将根据购物车总重量更改“统一费率”成本。

    使用“统一费率”运输方式,您需要使用简单的初始成本设置参考运输成本,而不是任何公式。例如,它可以是1。此费用将由我的答案代码替换,根据购物车总重量动态变化。

    您可能必须在“配送选项”标签下的常规配送设置中“启用调试模式”,才能暂时禁用配送缓存。

    add_filter('woocommerce_package_rates', 'shipping_cost_based_on_weight', 12, 2);
    function shipping_cost_based_on_weight( $rates, $package ){
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return $rates;
    
        // HERE define the differents costs
        $cost1 = 5; // Up to 6 Kg
        $cost2 = 9; // Above 6 Kg and below 12 kg
        $cost3 = 9; // Above 12 kg
    
        // The cart total Weight
        $total_weight = WC()->cart->get_cart_contents_weight();
    
        // Loop through the shipping taxes array
        foreach ( $rates as $rate_key => $rate ){
            $has_taxes = false;
            // Targetting "flat rate"
            if( 'flat_rate' === $rate->method_id ){
                // Get the initial cost
                $initial_cost = $new_cost = $rates[$rate_key]->cost;
    
                // Calculate new cost
                if( $total_weight <= 6 ) { // Below 6 Kg
                    $new_cost = $cost1;
                }
                elseif( $total_weight > 6 && $total_weight <= 12 ) { // Between 6 and 12 Kg
                    $new_cost = $cost2;
                }
                else { // Above 12 Kg
                    $new_cost = $cost3;
                }
    
                // Set the new cost
                $rates[$rate_key]->cost = $new_cost;
    
                // Taxes rate cost (if enabled)
                $taxes = [];
                // Loop through the shipping taxes array (as they can be many)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $rates[$rate_key]->taxes[$key] > 0 ){
                        // Get the initial tax cost
                        $initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
                        // Get the tax rate conversion
                        $tax_rate    = $initial_tax_cost / $initial_cost;
                        // Set the new tax cost
                        $taxes[$key] = $new_cost * $tax_rate;
                        $has_taxes   = true; // Enabling tax
                    }
                }
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        }
        return $rates;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    测试后,别忘了在发货设置中禁用“启用调试模式”选项。

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      相关资源
      最近更新 更多