Добавьте дополнительную плату за доставку по фиксированной ставке за каждые 3 товара в Woocommerce


Я управляю магазином woocommerce и использую доставку по фиксированной ставке $15. Я написал формулу для добавления $1.25 для каждого дополнительного элемента.

13.50 + ( 1.25 * [ кол-во])

Потягивание "настройки фиксированной ставки|$1.25 для дополнительного каждого элемента:

$1.25 for Additional Each Item

Но я хочу добавить эту стоимость $1.25 для каждых 3 предметов. Я имею в виду 3, 6, 9, 12 и так далее...

Кто-нибудь может сказать мне, как это сделать? Любая помощь будет оценена по достоинству.

Author: LoicTheAztec, 2018-07-04

2 answers

Следующий код добавит дополнительную стоимость к способу доставки по фиксированной ставке за каждые 3 товара (3, 6, 9 ...).

Вам нужно будет изменить стоимость доставки с помощью простой начальной стоимости вместо вашей формулы.

Возможно, вам придется "Включить режим отладки" в общих настройках доставки на вкладке "Параметры доставки", чтобы временно отключить кэш доставки.

Код (где вы укажете свою дополнительную доставку стоимость):

add_filter('woocommerce_package_rates', 'shipping_additional_cost_each_three_items', 12, 2);
function shipping_additional_cost_each_three_items( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    // HERE set your additional shipping cost
    $additional_cost = 1.25;
    $items_count = WC()->cart->get_cart_contents_count();

    // Loop through the shipping taxes array
    foreach ( $rates as $rate_key => $rate ){
        $has_taxes = false;
        // Targetting "flat rate"
        if( 'flat_rate' === $rate->method_id ){
            // Get the initial cost
            $initial_cost = $new_cost = $rates[$rate_key]->cost;
            // Adding to cost the additional cost each 3 items (3, 6, 9 …)
            for($i = 0; $i <= $items_count; $i+=3){
                $new_cost += $additional_cost;
            }
            // Set the new cost
            $rates[$rate_key]->cost = $new_cost;

            // Taxes rate cost (if enabled)
            $taxes = [];
            // Loop through the shipping taxes array (as they can be many)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $rates[$rate_key]->taxes[$key] > 0 ){
                    // Get the initial tax cost
                    $initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
                    // Get the tax rate conversion
                    $tax_rate    = $initial_tax_cost / $initial_cost;
                    // Set the new tax cost
                    $taxes[$key] = $new_cost * $tax_rate;
                    $has_taxes   = true; // Enabling tax
                }
            }
            if( $has_taxes )
                $rates[$rate_key]->taxes = $taxes;
        }
    }
    return $rates;
}

Вводится код function.php файл вашей активной дочерней темы (или активной темы). Проверено и работает.

Не забудьте отключить опцию "Включить режим отладки" в настройках доставки.


Ответ на основе вашего 2-го комментария:

Вы замените этот блок:

// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i+=3){
    $new_cost += $additional_cost;
}

Следующим образом:

// Adding to cost an additional fixed cost for the 2nd item
if($items_count >= 2){
    $new_cost += 6.21; 
}

// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i+=3){
    $new_cost += $additional_cost;
}
 1
Author: LoicTheAztec, 2018-07-29 01:37:24
<?php
$number = 13;
$all_number = 1;
for($i=1;$i<=13;$i++){
    if ($i % 3 == 0){
        $all_number = $all_number + 1;
    }
}
$price_new_data = $old_price*$all_number;
$price = 13.50+($price_new_data*$product_quenty);
?>
 -1
Author: Maulik patel, 2018-07-04 05:15:45