Максимальная стоимость доставки / WooCommerce / - PullRequest
0 голосов
/ 13 мая 2019

Во-первых, извините за мой английский.

У меня есть класс доставки, который называется Большие материалы, когда это 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 голосов
/ 14 мая 2019

Измените $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 );
...