0

在 Woocommerce 中,我想要一个可以包含在我的主题中的功能,它可以根据价格和重量增加运费。

  1. 如果价格高于 20 美元,则免运费,低于运费:3 美元
  2. 如果重量超过 10 公斤,运费是额外的 2 美元

基于根据物品重量和购物车数量计算的运费回答线程,我尝试了类似下面的代码:

//Adding a custom Shipping Fee to cart based conditionally on weight and cart amount
add_action('woocommerce_cart_calculate_fees', 'custom_conditional_shipping_fee', 10, 1);
function custom_conditional_shipping_fee( $cart_object ){

    #### SETTINGS ####

    // Your targeted "heavy" product weight
    $target_weight = 1;

    // Your targeted cart amount
    $target_cart_amount = 20;

    // Price by Kg;
    $price_kg = 2;

    // Amount set in 'flat rate' shipping method;
    $flat_rate_price;


    // Initializing variables
    $fee = 0;
    $calculated_weight = 0;

    // For cart SUBTOTAL amount EXCLUDING TAXES
    WC()->cart->subtotal_ex_tax >= $target_cart_amount ? $passed = true : $passed = false ;

    // For cart SUBTOTAL amount INCLUDING TAXES (replace by this):
    // WC()->cart->subtotal >= $target_cart_amount ? $passed = true : $passed = false ;

    // Iterating through each cart items
    foreach( $cart_object->get_cart() as $cart_item ){

        // Item id ($product ID or variation ID)
        if( $cart_item['variation_id'] > 0)
            $item_id = $cart_item['variation_id'];
        else
            $item_id = $cart_item['product_id'];

        // Getting the product weight
        $product_weight = get_post_meta( $item_id , '_weight', true);

        // Line item weight
        $line_item_weight = $cart_item['quantity'] * $product_weight;

        // When cart amount is up to 1kg, Adding weight of heavy items
        if($passed && $product_weight < $target_weight)
            $calculated_weight += $line_item_weight;
    }

    #### Making the fee calculation ####

    // Cart is up to 250 with heavy items
    if ( $passed && $calculated_weight != 0 ) {
        // Fee is based on cumulated weight of heavy items
        $fee = ($calculated_weight * $price_kg) - $flat_rate_price;
    }
    // Cart is below 250
    elseif ( !$passed ) {
        // Fee is based on cart total weight
        $fee = ($cart_object->get_cart_contents_weight( ) * $price_kg) - $flat_rate_price;
    }

    #### APPLYING THE CALCULATED FEE ####

    // When cart is below 250 or when there is heavy items
    if ($fee > 0){
        // Rounding the fee
        $fee = round($fee);
        // This shipping fee is taxable (You can have it not taxable changing last argument to false)
        $cart_object->add_fee( __('Shipping weight fee', 'woocommerce'), $fee, true);
    }
}

编辑:

同时我希望它立即在购物车页面上显示。现在它显示“输入地址以查看运输选项”。基本上只需查看购物车总数,并根据重量和价格描述的规则显示费率或免费送货。

输入地址以查看运输选项

4

2 回答 2

2

woocommerce_package_rates是自定义运费的正确过滤器。

您可以通过以下方式实现此目的。

第 1 步:创建两种送货方式,免费送货统一费率,海岸 3 美元

第 2 步:将以下代码片段复制并粘贴到 functions.php 中

在代码段上正确配置统一费率和免费送货。

add_filter( 'woocommerce_package_rates',  'modify_shipping_rate', 15, 2 );
function modify_shipping_rate( $available_shipping_methods, $package ){

    global $woocmmerce;
    $total_weight = WC()->cart->cart_contents_weight;
    $total_coast = WC()->cart->get_cart_contents_total();


    if( $total_coast >= 20 ){
        unset($available_shipping_methods['flat_rate:1']); //Remove flat rate for coat abobe 20$
    }elseif( $total_weight > 10 ){
        unset($available_shipping_methods['free_shipping:1']); // remove free shipping for below 20$ 
        $available_shipping_methods['flat_rate:1']->cost += 2; // add 2$ if weight exceeds 10KG
    }else{
        unset($available_shipping_methods['free_shipping:1']); // remove free shipping for below 20$ 
    }

    return $available_shipping_methods;
}

使用以下代码段更改默认购物车消息。

add_filter( 'woocommerce_cart_no_shipping_available_html', 'change_msg_no_available_shipping_methods', 10, 1  );
add_filter( 'woocommerce_no_shipping_available_html', 'change_msg_no_available_shipping_methods', 10, 1 );
function change_msg_no_available_shipping_methods( $default_msg ) {

    $custom_msg = "Enter address to view shipping options";
    if( empty( $custom_msg ) ) {
      return $default_msg;
    }

    return $custom_msg;
}
于 2019-03-07T11:09:31.960 回答
0

以下代码不是基于自定义费用,而是基于运输方式自定义。它需要在运输设置中针对每个运输区域进行设置:

  • 具有明确成本的“统一费率” $3为您)
  • 没有要求(没有限制)的“免费送货” 。

该代码将根据重量和税收计算处理统一费率计算成本。

该代码适用于任何运输区域,无需在代码中定义运输方式 ID。

这是代码:

add_filter( 'woocommerce_package_rates',  'filter_package_rates_callback', 10, 2 );
function filter_package_rates_callback( $rates, $package ) {

    ## -------- Settings -------- ##
    $targeted_total   = 20; // The targeted cart amount
    $weight_threshold = 10; // The cart weight threshold
    $extra_for_10kg   = 2;  // 10 Kg addition extra cost;

    $total_weight    = WC()->cart->get_cart_contents_weight();
    $cart_subtotal   = WC()->cart->get_subtotal(); // Excluding taxes

    // Set shipping costs based on weight
    foreach ( $rates as $rate_key => $rate ){
        $has_taxes = false;

        if( $cart_subtotal < $targeted_total || $total_weight >= $weight_threshold ){
            // Remove Free shipping Method
            if( 'free_shipping' === $rate->method_id ) {
                unset( $rates[$rate_key] );
            }
            // Flat rate calculation cost when 10 kg weight is reached
            if( 'flat_rate' === $rate->method_id && $total_weight >= $weight_threshold ) {
                // The default rate cost (set in the shipping method)
                $default_cost = $rate->cost; 

                // The new calculated cost (up to 10 kg)
                $new_cost = $default_cost + $extra_for_10kg; 

                // Tax rate conversion (for tax calculations)
                $tax_rate_converion = $new_cost / $default_cost;

                // Set the new cost
                $rates[$rate_key]->cost = $new_cost;

                // TAXES RATE COST (if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $tax > 0 ){
                        // New tax calculated cost
                        $taxes[$key] = $tax * $tax_rate_converion; 
                        $has_taxes = true;
                    }
                }
                // Set new taxes cost
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        } else {
            // Remove Flat Rate methods (keeping Free Shipping Method only)
            if( 'flat_rate' === $rate->method_id ) {
                unset( $rates[$rate_key] );
            }
        }
    }
    return $rates;
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。

刷新运输缓存:( 必需)
1) 此代码已保存在您的 function.php 文件中。
2)在运输区域设置中,禁用/保存任何运输方式,然后启用返回/保存。
你已经完成了,你可以测试它。


直接显示运输方式 (关于您的问题编辑)

您问题中提供的信息不足以了解如何管理。

如果您在一个国家/地区销售,并且您有一个独特的运输区域,您可以强制未登录客户的国家/地区显示运输方式,使用以下方法:

add_action( 'template_redirect', 'allow_display_shipping_methods' );
function allow_display_shipping_methods() {
    // HERE define the targeted country code
    $country_code = 'GB';

    // Set the shipping country if it doesn't exist
    if( ! WC()->customer->get_shipping_country() )
        WC()->customer->set_shipping_country('GB');
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。

现在这是另一个问题,在您最初的问题中,应该作为一个新问题提出


相关答案:

于 2019-03-07T19:50:22.823 回答