douoyou3348 2019-05-12 06:16
浏览 533

在WooCommerce中设置每个订单项的运费

There is the built in option for woocommerce shipping to set a fee for quantity of products in cart. There is also option for fee per shipping class. But I have thousands of products and need to charge a fee for each product(each product not qty of products).

For example 2 product "apples", and 23 product "oranges" in cart. I need to charge flat fee of $10 for any amount of apples and $10 for any amount of oranges. I dont seem to find solution to this in any of the available plugins. They all do fee per quantity but not this.

  • 写回答

1条回答 默认 最新

  • dragon8997474 2019-05-12 16:16
    关注

    To get a cost by line item in cart, it requires the following:

    1) In WooCommerce Settings > Shipping: set a cost of 10 for your "Flat rate" shipping methods (and save).

    2) Add to functions.php file of your active child theme (or active theme), this code:

    add_filter( 'woocommerce_package_rates', 'shipping_cost_based_on_number_of_items', 10, 2 );
    function shipping_cost_based_on_number_of_items( $rates, $package ) {
        $numer_of_items = (int) sizeof($package['contents']);
    
        // Loop through shipping rates
        foreach ( $rates as $rate_key => $rate ){
            // Targetting "Flat rate" shipping method
            if( 'flat_rate' === $rate->method_id ) {
                $has_taxes = false;
    
                // Set the new cost
                $rates[$rate_key]->cost = $rate->cost * $numer_of_items;
    
                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $tax > 0 ){
                        // New tax calculated cost
                        $taxes[$key] = $tax * $numer_of_items;
                        $has_taxes = true;
                    }
                }
                // Set new taxes cost
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        }
        return $rates;
    }
    

    Refresh the shipping caches: (required)

    1. This code is already saved on your active theme's function.php file.
    2. The cart is empty
    3. In a shipping zone settings, disable / save any shipping method, then enable back / save.

    Tested and work.

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答