【问题标题】:WooCommerce shipping cost based on cart item count基于购物车商品数量的 WooCommerce 运费
【发布时间】:2018-01-30 13:29:30
【问题描述】:

我想根据添加到购物车的产品数量来计算运费,例如,

如果我购买一部手机,那么运费将计为 2.5,而在我购买超过两部或两部手机后,运费将是 5.0

 <?php

      $qty(1) * 2.5 = 2.5

      $qty(2) * 2.5 = 5.0

      $qty(3) * 2.5 = 5.0

  ?>

那么有什么想法或建议如何根据产品数量计算运费吗?

【问题讨论】:

  • 您需要为此创建自定义运输方式 - 我认为此链接对您有帮助 code.tutsplus.com/tutorials/…
  • @LoicTheAztec 很抱歉回复你晚了,但我也通过自定义代码尝试了很多方法,但我没有成功,所以我找到了它的插件。

标签: php wordpress woocommerce cart shipping


【解决方案1】:

更新:

由于您的问题有点不清楚,您只需在Flat rate运输方式cost中添加[qty]*2.5(对于每个运输区域)在您的 wooCommerce 运输设置中。

但如果您的购物车中有 2 件不同的商品,它将无法工作,例如:item1 (qty 1) + item2 (qty 1)

所以这个答案适用于所有情况:

1) 首先,您需要为每个运输区域设置 “统一费率” 运输方式,其成本将设置为 2.5(在您的 WooCommerce 运输中设置)。

2) 添加此代码,将计算每个购物车商品(基于商品总数量)的新更新运费:

add_filter( 'woocommerce_package_rates', 'custom_flat_rate_cost_calculation', 10, 2 );
function custom_flat_rate_cost_calculation( $rates, $package )
{
    // The cart count (total items in cart)
    $cart_count = WC()->cart->get_cart_contents_count();
    $taxes = array();

    // If there is more than 1 cart item
    if( $cart_count > 1 ){
        // Iterating through each shipping rate
        foreach($rates as $rate_key => $rate_values){
            // Targeting "Flat Rate" shipping method
            if ( 'flat_rate' === $rate_values->method_id ) {
                // Set the new calculated rate cost
                $rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cumulated_active_quantity, 2);
                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_id]->taxes as $key => $tax){
                    if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
                        $taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cumulated_active_quantity, 2 );
                        $has_taxes = true;
                    } else {
                        $has_taxes = false;
                    }
                }
                if( $has_taxes )
                    $rates[$rate_id]->taxes = $taxes; 
            }
        }
    }
    return $rates;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码在 WooCommerce 3+ 上经过测试并且有效

您需要刷新运输区域缓存:禁用统一费率,然后保存。并启用此速率并保存。

【讨论】:

    【解决方案2】:

    您可以添加自定义费用:添加到主题 functions.php 或使用插件

    add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
    
    function woocommerce_custom_surcharge() {
      global $woocommerce;
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        $price_per_mobile = 2.5;
        $shipcharge = ( $woocommerce->cart->cart_contents_total * $price_per_mobile);   
        $woocommerce->cart->add_fee( 'Total Shipping Cost', $shipcharge, true, '' );
    
    }
    

    【讨论】:

    • 感谢我有一个插件,所以它现在可以工作了,但感谢您与我们分享答案。
    【解决方案3】:

    继续使用 Woocommerce 过滤器woocommerce_package_rates。您可以在其中自定义购物车页面中所有可用的运费。

    这里是为国内和国际运输的所有物品添加额外费用的代码

    add_filter('woocommerce_package_rates', 'wf_modify_rate', 10, 3);
    function wf_modify_rate( $available_shipping_methods, $package ){
        $origin_country = 'US';
        $amount_to_add_domestic = 10;
        $amount_to_add_inter_national = 20;
    
        $amount_to_add = ($package['destination']['country'] == $origin_country) ? 
        $amount_to_add_domestic : $amount_to_add_inter_national;
    
        $item_count = 0;
        foreach ($package['contents'] as $key => $item) {
            $item_count += $item['quantity'];
        }
    
        foreach ($available_shipping_methods as $methord_name => $methord) {
            $available_shipping_methods[$methord_name]->cost += ($amount_to_add*$item_count);
        }
        return $available_shipping_methods;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 2020-10-12
      • 1970-01-01
      • 2017-06-05
      • 2019-01-23
      • 2018-04-10
      • 2018-02-05
      • 1970-01-01
      • 2018-02-10
      相关资源
      最近更新 更多