簡體   English   中英

為 Woocommerce 中每 3 件商品的統一運費添加額外費用

[英]Add an additional cost to flat rate shipping each 3 items in Woocommerce

我正在經營一家 woocommerce 商店並使用統一運費$15 我編寫了一個公式,為每個額外項目添加$1.25

13.50 + ( 1.25 * [數量])

啜飲“統一費率設置 | 額外每件商品$1.25

每件商品另加 1.25 美元

但我想為每 3 件商品添加$1.25 我的意思是 3、6、9、12 等等......

誰能告訴我如何做到這一點? 任何幫助表示贊賞。

更新(2021)

以下代碼將為每 3 件(3, 6, 9 …) 的統一費率運輸方式增加額外費用。

您需要使用簡單的初始成本而不是公式來更改運輸成本。

您可能必須在“運輸選項”選項卡下的一般運輸設置中“啟用調試模式”,以禁用臨時運輸緩存。

代碼(您將在其中設置額外運費)

add_filter('woocommerce_package_rates', 'shipping_additional_cost_each_three_items', 10, 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     = 0; // Initializing
    $each_items      = 3; // Number of items (for additional cost)

    // Loop through cart items for the current shipping package        
    foreach( $package['contents'] as $cart_item ) {
        $items_count = += $cart_item['quantity']; // Count cart items for current shipping package
    }

    if ( $items_count >= $each_items ) {
        // Loop through the shipping taxes array
        foreach ( $rates as $rate_key => $rate ){
            // Targetting "flat rate"
            if( 'flat_rate' === $rate->method_id ){
                $initial_cost = $new_cost = $rate->cost;
                $has_taxes    = false; // Initializing
                $taxes        = array(); // Initializing
                
                // Adding to cost the additional cost each 3 items (3, 6, 9 …)
                for($i = 0; $i <= $items_count; $i+ = $each_items){
                    $new_cost += $additional_cost;
                }
                $rates[$rate_key]->cost = $new_cost; // Set the new cost
    
                // Taxes rate cost (if any) - Loop through taxes array (as they can be many)
                foreach ($rate->taxes as $key => $tax){
                    if( $tax > 0 ){
                        // Get the initial tax cost
                        $initial_tax_cost = $new_tax_cost = $tax;
                        // 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 changes
                    }
                }
                // set array of shipping tax cost
                if( $has_taxes ) {
                    $rates[$rate_key]->taxes = $taxes; 
                }
            }
        }
    }
    return $rates;
}

代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。

不要忘記在運輸設置中禁用“啟用調試模式”選項。


根據您的第二條評論回答:

您將替換此塊:

// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i += $each_items){
    $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 += $each_items){
    $new_cost += $additional_cost;
}
<?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);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM