基于购物车在 WooCommerce 中的小计百分比的累进固定费率

Progressive flat rate based on the cart's subtotal percentage in 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 
        $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$ 的运费。

First you need to set a cost of 5 (and also "UPS" as label) in your Flat rate settings.

然后使用此代码:

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 文件。已测试并有效。

Refresh the shipping caches:

  1. This code is already saved on your functions.php file.
  2. In a shipping zone settings, disable / save any shipping method, then enable back / save.

    You are done and you can test it.