-1

如何在 Woocommerce 中根据购物车小计使用不同的运费?

例如:

  • 如果小计小于 5000,则统一费率成本为 250。
  • 如果小计小于 2000,则统一费率成本为 150。
  • 如果小计超过 5000 则不收取运费。

如果可能的话,我想为特定产品收取 500 美元的费用。

4

1 回答 1

1

尝试以下功能,其中“统一费率”成本将根据您问题中定义的购物车小计进行更改。

如果购物车小计达到 5000,我们将隐藏“统一费率”。您将需要启用“最低订单金额”选项为 5000 的免费送货方式。

在此处输入图像描述

使用“统一费率”运输方式,您需要使用简单的初始成本而不是任何公式来设置参考运输成本。它可以是例如1。此费用将由我的答案代码替换,根据购物车总重量动态。

在此处输入图像描述

它还将处理特定的产品 ID,如果在购物车中,则将成本设置为 500。

您可能必须在“运输选项”选项卡下的常规运输设置中“启用调试模式”,以暂时禁用运输缓存。

编码:

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

    // HERE define the differents costs
    $cost1 = 150; // Below 2000
    $cost2 = 250; // Below 5000
    $cost3 = 500; // Cost for our specific product ID

    $step1_subtotal = 2000;
    $max_subtotal   = 5000;


    // HERE DEFINE the specific product ID to be charged at 500
    $targeted_product_id = 37;

    // The cart subtotal
    $subtotal = WC()->cart->get_subtotal();

    // Loop through cart items and checking for the specific product
    $found = false;
    foreach( $package['contents'] as $item ) {
        if( $item['product_id'] == $targeted_product_id || $item['variation_id'] == $targeted_product_id ){
            $found = true;
            break;
        }
    }

    // Loop through the shipping taxes array
    foreach ( $rates as $rate_key => $rate ){
        $has_taxes = false;

        // If subtotal is up to 5000 we enable free shipping only
        if( 'free_shipping' !== $rate->method_id && $subtotal >= $max_subtotal ){
            unset($rates[$rate_key]);
        }
        // Targetting "flat rate" only for subtotal below 5000
        else if( 'flat_rate' === $rate->method_id && $subtotal < $max_subtotal ){
            // Get the initial cost
            $initial_cost = $new_cost = $rates[$rate_key]->cost;

            // Calculate new cost
            if( $subtotal < $step1_subtotal ) { // Below 2000
                $new_cost = $cost1;
            }
            elseif( $subtotal >= $step1_subtotal && $subtotal < $max_subtotal ) { // Between 2000 and below 5000
                $new_cost = $cost2;
            }

            // For the specific product ID (if found in cart items)
            if( $found ){
                $new_cost = $cost2;
            }

            // 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 文件中。测试和工作。

测试后,不要忘记在发货设置中禁用“启用调试模式”选项。

于 2018-08-17T12:09:12.250 回答