【问题标题】:Free shipping depending on weight and on minimal cart amount免费送货取决于重量和最小的购物车数量
【发布时间】:2017-07-07 01:29:15
【问题描述】:

使用 WooCommerce,我需要超过一定数量的 250 免费送货,但购物车中包含的重型产品除外。

有人知道我应该怎么做吗?

谢谢。

【问题讨论】:

  • 所以您希望购物车中的某些商品免运费,而其他商品不免费?
  • 请根据minimal reproducible example编辑您的问题
  • 嘿,是的。我只需要 250 件以上的轻型产品免费送货。我需要运输成本超过 250 个限制的重型产品。
  • 看看我的回答:stackoverflow.com/questions/15457059/…。不完全相同,但非常相似.. 应该可以帮助您指出正确的方向

标签: php wordpress woocommerce checkout shipping


【解决方案1】:

此自定义代码将保留 免费送货 方式,并在购物车数量达到 250 且产品不重 时隐藏其他送货方式strong>(这里小于 20 公斤)...为了不让小于 250 的订单免运费,您可以在 woocommerce 中设置此项(见最后)。

首先,您必须确保在每个重型产品中设置了重量(对于简单或可变产品(在每个变体中)。此处的购物车小计是不含税 (并且您可以轻松地将其更改为包括税费)

然后这是 woocommerce_package_rates 过滤器挂钩中的自定义挂钩函数:

add_filter( 'woocommerce_package_rates', 'conditionally_hide_other_shipping_based_on_items_weight', 100, 1 );
function conditionally_hide_other_shipping_based_on_items_weight( $rates ) {

    // Set HERE your targeted weight (here is 20 kg) <== <== <== <== <==
    $targeted_product_weight = 20;
    
    // Set HERE your targeted cart amount (here is 250)  <== <== <== <== <==
    $targeted_cart_amount = 250;
    // For cart subtotal amount EXCLUDING TAXES
    $passed = WC()->cart->subtotal_ex_tax >= $targeted_cart_amount ? true : false;
    // For cart subtotal amount INCLUDING TAXES (replace by this):
    // $passed = WC()->cart->subtotal >= $targeted_cart_amount ? true : false;
    $light_products_only = true; // Initializing

    // Iterating trough cart items to get the weight for each item
    foreach( $package['contents'] as $cart_item ){
        // Getting the product weight
        $product_weight = $cart_item['data']->get_weight();

        if( !empty($product_weight) && $product_weight >= $targeted_product_weight ){
            $light_products_only = false;
            break;
        }
    }

    // If 'free_shipping' method is available and if products are not heavy
    // and cart amout up to the target limit, we hide other methods
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id && $passed && $light_products_only ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }

    return ! empty( $free ) ? $free : $rates;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码经过测试,适用于简单且可变的产品……

您还必须在 WooCommerce 设置 > 运输中,为每个运输区域和 "Free Shipping" 方法设置您的最低订购量:

您需要刷新运输缓存数据:在woocommerce运输设置中禁用、保存和启用、保存当前运输区域的相关运输方式。

【讨论】:

  • 非常感谢您的宝贵时间!!这是否只会给重的物品每公斤超过 250 的运费,而包含在购物车中的“轻”物品将免费送货?
  • 我不明白,我对此感到抱歉...那么,在购物车上较重的物品和较轻的物品会发生什么?他们会按照我的意愿收取运费吗?
【解决方案2】:

对于超过一定数量的免费送货,您可以使用内置的 WooCommerce 选项。

对于某些特定产品可以跳过免费送货,您可以使用下面的sn-p。

 add_filter('woocommerce_package_rates', 'hide_shipping_method_if_particular_product_available_in_cart', 10, 2);

        function hide_shipping_method_if_particular_product_available_in_cart($available_shipping_methods)
        {
            global $woocommerce;

            // products_array should be filled with all the products ids
            // for which shipping method (stamps) to be restricted.

            $products_array = array(
                101,
                102,
                103,
                104
            );

            // You can find the shipping service codes by doing inspect element using
            // developer tools of chrome. Code for each shipping service can be obtained by
            // checking 'value' of shipping option.

            $shipping_services_to_hide = array(
                'free_shipping',

            );

            // Get all products from the cart.

            $products = $woocommerce->cart->get_cart();

            // Crawl through each items in the cart.

            foreach($products as $key => $item) {

                // If any product id from the array is present in the cart,
                // unset all shipping method services part of shipping_services_to_hide array.

                if (in_array($item['product_id'], $products_array)) {
                    foreach($shipping_services_to_hide as & $value) {
                        unset($available_shipping_methods[$value]);
                    }

                    break;
                }
            }

            // return updated available_shipping_methods;
    return
        }

【讨论】:

  • 嘿,谢谢你的 sn-p。我只需要 250 以上的轻型产品免费送货。我需要运输成本超过 250 限制的重型产品。那个 sn-p 包括那个吗?非常感谢
  • 可以通过 Woocommerce 设置特定最低金额的免费送货。
  • 重产品ids可以添加到sn-p中,可以稍微修改管理运输
  • $products_array = array( 101, 102, 103, 104 你的意思是对吗?所以在这里添加 id 重的物品只会有超过 250 的运费,轻的在我之后将免费放在厕所里?对不起,如果这听起来很愚蠢
猜你喜欢
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多