最高运费/ woocommerce /

时间:2019-05-13 08:10:20

标签: wordpress shipping

首先,对不起我的英语。

我有一个叫做Big Materials的运输舱,当修理10€时每件加2€。我已将此代码设置为24欧元的最高运费。

当我将8个购物车放入购物车时,运费为26€。当我将9件商品放入购物车时,运费为28欧元,当我将10件商品放入购物车时,运费为我设置的24欧元。

我不知道,问题出在哪里。

是在税还是…?

你能帮我吗?

谢谢。

/**
 * Function to set a minimum/cap on the shipping rates.
 */
function my_minimum_limit_shipping_rates_function( $rates, $package ) {
    $methods = WC()->shipping()->get_shipping_methods();
    $shipping_limit = 24; // The maximum amount would be $24
    // Loop through all rates
    foreach ( $rates as $rate_id => $rate ) {
        // Check if the rate is higher then a certain amount
        if ( $rate->cost > $shipping_limit ) {
            $rate->cost = $shipping_limit;
            // Recalculate shipping taxes
            if ( $method = $methods[ $rate->get_method_id() ] ) {
                $taxes = WC_Tax::calc_shipping_tax( $rate->cost, WC_Tax::get_shipping_tax_rates() );
                $rate->set_taxes( $method->is_taxable() ? $taxes : array() );
            }
        }
    }
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'my_minimum_limit_shipping_rates_function', 10, 2 );

1 个答案:

答案 0 :(得分:0)

$rate->cost更改为$rates[$rate_id]并检查

/**
 * Function to set a minimum/cap on the shipping rates.
 */
function my_minimum_limit_shipping_rates_function( $rates, $package ) {
    $methods = WC()->shipping()->get_shipping_methods();
    $shipping_limit = 24; // The maximum amount would be $24
    // Loop through all rates
    foreach ( $rates as $rate_id => $rate ) {
        // Check if the rate is higher then a certain amount
        if ( $rate->cost > $shipping_limit ) {
            $rates[$rate_id]->cost = $shipping_limit;                
        }
    }
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'my_minimum_limit_shipping_rates_function', 10, 2 );