免运费取决于重量和最小的购物车数量

spy*_*spy 5 php wordpress checkout shipping woocommerce

有了WooCommerce,除了购物车中包含的重型产品外,我需要超过250的免费送货.

有谁知道我应该怎么做?

谢谢.

Loi*_*tec 3

此自定义代码将保留免费送货方式,并在购物车数量达到250 件且产品不重(此处小于 20 公斤)时隐藏其他送货方式\xe2\x80\xa6 不允许订单小于 1 的免费送货250,你可以在woocommerce中设置这个(见最后)。

\n

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

\n

在此输入图像描述

\n

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

\n
add_filter( \'woocommerce_package_rates\', \'conditionally_hide_other_shipping_based_on_items_weight\', 100, 1 );\nfunction conditionally_hide_other_shipping_based_on_items_weight( $rates ) {\n\n    // Set HERE your targeted weight (here is 20 kg) <== <== <== <== <==\n    $targeted_product_weight = 20;\n    \n    // Set HERE your targeted cart amount (here is 250)  <== <== <== <== <==\n    $targeted_cart_amount = 250;\n    // For cart subtotal amount EXCLUDING TAXES\n    $passed = WC()->cart->subtotal_ex_tax >= $targeted_cart_amount ? true : false;\n    // For cart subtotal amount INCLUDING TAXES (replace by this):\n    // $passed = WC()->cart->subtotal >= $targeted_cart_amount ? true : false;\n    $light_products_only = true; // Initializing\n\n    // Iterating trough cart items to get the weight for each item\n    foreach( $package[\'contents\'] as $cart_item ){\n        // Getting the product weight\n        $product_weight = $cart_item[\'data\']->get_weight();\n\n        if( !empty($product_weight) && $product_weight >= $targeted_product_weight ){\n            $light_products_only = false;\n            break;\n        }\n    }\n\n    // If \'free_shipping\' method is available and if products are not heavy\n    // and cart amout up to the target limit, we hide other methods\n    $free = array();\n    foreach ( $rates as $rate_id => $rate ) {\n        if ( \'free_shipping\' === $rate->method_id && $passed && $light_products_only ) {\n            $free[ $rate_id ] = $rate;\n            break;\n        }\n    }\n\n    return ! empty( $free ) ? $free : $rates;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

此代码经过测试,适用于简单和可变的产品\xe2\x80\xa6

\n
\n

您还必须在 WooCommerce 设置 > 运输中,针对每个运输区域以及"Free Shipping"最低订单金额的方法:

\n
\n

在此输入图像描述

\n
\n

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

\n
\n