基于WooCommerce中购物车小计百分比的累进统一费率

问题描述

在WooCommerce中,我试图根据购物车小计的10%(最低费用为5美元,最高费用为11美元)设置累进运费。

这是我的代码尝试:

add_filter( 'woocommerce_package_rates','woocommerce_package_rates',10,2 );
function woocommerce_package_rates( $rates,$package ) {
    // Make sure flat rate is available
    if ( isset( $rates['flat_rate:24'] ) ) {
        // Set the cost to $5
        $rates['flat_rate:24']->cost = 5;
    }
    $cart_subtotal = $WC()->cart->subtotal
    if ($cart_subtotal >50)

    $percentage = 0.10;  // Percentage (10%) in float
    $percentage_fee = ( WC()->cart->subtotal >+ WC()->cart->get_shipping_total()) * $percentage;
    }
 
    });
    return $rates;
}

此代码在网站上出现严重错误。我对Wordpress自定义编码非常陌生,您可能会从下面的代码中得知。

我还希望显示的标签显示“ USPS”而不是“固定费率”。

例如,如果购物车小计为60美元,则将向他们收取6美元的统一运费(小计的10%)。

我想念什么或做错什么了?

解决方法

您提供的代码中有很多错误……以下内容使您可以根据购物车小计百分比,将运费设置为从5$11$最大。

首先,您需要在统一费率设置中将费用设置为5(并且还将“ UPS”作为标签)设置。

enter image description here

然后使用此代码:

add_filter( 'woocommerce_package_rates','woocommerce_package_rates',10,2 );
function woocommerce_package_rates( $rates,$package ) {
    $max_cost   = 11; // Here set the max cost for the shipping method
    $percentage = 10; // Percentage to apply on cart subtotal
    $subtotal   = WC()->cart->get_subtotal(); // Cart subtotal without taxes

    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ) {
        // Targetting specific flate rate shipping method
        if ( 'flat_rate:14' === $rate_key ) {
            $has_taxes = false;
            $base_cost = $rate->cost; // 5$ from this shipping method cost setting
            $new_cost  = $subtotal * $percentage / 100; // Calculation

            if( $new_cost > $base_cost ) {
                // 1. Rate cost
                if ( $new_cost < $max_cost ) {
                    $rates[$rate_key]->cost = $new_cost;
                    $rate_operand = $new_cost / $base_cost; // (for taxes if enabled)
                } else {
                    $rates[$rate_key]->cost = $max_cost;
                    $rate_operand = $max_cost / $base_cost; // (for taxes if enabled)
                }
                // 2. Taxes rate cost (if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $tax > 0 ){
                        // New tax calculated cost
                        $taxes[$key] = $tax * $rate_operand;
                        $has_taxes = true;
                    }
                }
                // Set new taxes cost
                if( $has_taxes ) {
                    $rates[$rate_key]->taxes = $taxes;
                }
            }
        }
    }
    return $rates;
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

刷新发货缓存:

  1. 此代码已保存在您的functions.php文件中。
  2. 在运输区域设置中,禁用/保存任何运输方式,然后启用回退/保存。

    您已完成,您可以对其进行测试。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...