簡體   English   中英

在 WooCommerce 中設置每個訂單項的運費

[英]Set a shipping cost per line item in WooCommerce

woocommerce 航運有一個內置選項,可以為購物車中的產品數量設置費用。 還有每個運輸類別的費用選項。 但是我有成千上萬的產品,需要為每個產品收取費用(每個產品不是產品數量)。

例如,購物車中有 2 個產品“蘋果”和 23 個產品“橙子”。 我需要對任意數量的蘋果收取 10 美元的固定費用,對任意數量的橙子收取 10 美元的固定費用。 我似乎沒有在任何可用的插件中找到解決方案。 他們都按數量收費,但不是這個。

要按購物車中的訂單項獲取成本,它需要以下內容:

1) 在 WooCommerce 設置 > 運輸中:為您的“統一費率”運輸方式設置 10 的成本(並保存)

2)添加到您的活動子主題(或活動主題)的functions.php文件中,此代碼:

add_filter( 'woocommerce_package_rates', 'shipping_cost_based_on_number_of_items', 10, 2 );
function shipping_cost_based_on_number_of_items( $rates, $package ) {
    $numer_of_items = (int) sizeof($package['contents']);

    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ){
        // Targetting "Flat rate" shipping method
        if( 'flat_rate' === $rate->method_id ) {
            $has_taxes = false;

            // Set the new cost
            $rates[$rate_key]->cost = $rate->cost * $numer_of_items;

            // Taxes rate cost (if enabled)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $tax > 0 ){
                    // New tax calculated cost
                    $taxes[$key] = $tax * $numer_of_items;
                    $has_taxes = true;
                }
            }
            // Set new taxes cost
            if( $has_taxes )
                $rates[$rate_key]->taxes = $taxes;
        }
    }
    return $rates;
}

刷新運輸緩存:(必需)

  1. 此代碼已保存在活動主題的 function.php 文件中。
  2. 購物車是空的
  3. 在運輸區域設置中,禁用/保存任何運輸方式,然后啟用返回/保存。

測試和工作。

暫無
暫無

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

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