簡體   English   中英

在 WooCommerce 中按運輸區域設置最小和最大運費成本

[英]Set a min and max shipping rate cost by shipping zone in WooCommerce

我目前有 2 個運輸區域(法國、歐洲),每個區域都有多種交付選項,包括統一費率。 這些統一費率選項有一個不同的公式來計算基於項目數量的費用,其中 AB 和 C 三個不同的運費 class。 使用 Woocommerce 自帶的高級成本選項很容易做到。

我的問題是我想為送貨費設置一個最小值和最大值,而這些最小值/最大值僅適用於統一費率選項,並且因地理區域而異。 簡而言之:

  • 法國統一費率法: 2€ + (1€*[qtyA] + 2€*[qtyB] + 5€*[qtyC]) Min = 7Max = 15
  • 歐洲統一費率法: 6€ + (2€*[qtyA] + 4€*[qtyB] + 8€*[qtyC]) Min = 10Max = 25

我嘗試在 function.php 中編寫一些代碼,條件取決於運輸區域和運輸方式,但我的最小值或最大值均未應用。

希望也許有人能提供幫助,因為這讓我發瘋。

function maximum_shipping_rates_function( $rates, $package ) {

    $methods = WC()->shipping()->get_shipping_methods();
    $shipping_limit = 10.50; // I set my minimul to 10.50€
    $only_apply_to_rates = array( 'shipping_method_0_flat_rate2', 'flat_rate' ); // I put here the Shipping method ID thqt I get by inspecting the option on the checkout page

    // Loop through all rates
    foreach ( $rates as $rate_id => $rate ) {
        
        // Skip the shipping rates that are not in the list
        if ( ! in_array( $rate_id, $only_apply_to_rates ) ) {
            continue;
        }
        
        // Check if the rate is higher than my maximum
        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_action( 'woocommerce_package_rates', 'maximum_shipping_rates_function', 10, 2 );

要按運輸區域名稱設置不同的最小和最大運輸成本,請改用以下內容:

add_filter( 'woocommerce_package_rates', 'specificproductsshipping_methods', 10, 2 );
function specificproductsshipping_methods( $rates, $package ){
    $first_rate  = reset($rates); // get first rate (to get the shipping zone from)
    $chosen_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $first_rate->instance_id ); // Current Shipping Zone Object
    $zone_name   = $chosen_zone->get_zone_name(); // Get shipping zone name

    // Loop through all rates
    foreach ( $rates as $rate_key => $rate ) {
        // Targeting "Flat rate" shipping methods only
        if ( 'flat_rate' === $rate->method_id ) {
            $initial_cost = $new_cost = $rate->cost;

            // 1. For "France" shipping zone name (set min and max cost)
            if ( 'France' === $zone_name ) {
                if ( $initial_cost < 7 ) {
                    $new_cost = 7;
                } elseif ( $initial_cost > 15 ) {
                    $new_cost = 15;
                }
            }
            // For "Europe" shipping zone name (set min and max cost)
            elseif ( 'Europe' === $zone_name ) {
                if ( $initial_cost < 10 ) {
                    $new_cost = 10;
                } elseif ( $initial_cost > 25 ) {
                    $new_cost = 25;
                }
            }

            if ( $new_cost != $initial_cost ) {
                $rates[$rate_key]->cost = $new_cost; // Set the new cost

                $taxes = []; // Initializing

                // Taxes: Loop through the shipping taxes array (as they can be many)
                foreach ($rate->taxes as $key => $tax){
                    if( $tax > 0 ){
                        $initial_tax_cost = $tax; // Get the initial tax cost

                        $tax_rate    = $initial_tax_cost / $initial_cost; // Get the tax rate conversion

                        $taxes[$key] = $new_cost * $tax_rate; // Set the new tax cost in taxes costs array
                        $has_taxes   = true; // Enabling tax
                    }
                }
                if( isset($has_taxes) && $has_taxes ) {
                    $rates[$rate_key]->taxes = $taxes; // Set taxes costs array
                }
            }
        }
    }
    return $rates;
}

代碼進入活動子主題(或活動主題)的functions.php文件。 測試和工作。

不要忘記清空您的購物車以刷新運輸方式緩存。

暫無
暫無

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

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