問題描述
使用 WooCommerce,我需要獲得超過 250 的免費(fèi)送貨服務(wù),但購物車中包含的重型產(chǎn)品除外.
With WooCommerce, I need to have Free Shipping over certain amount of 250 except the heavy products that are included in the cart.
有人知道我該怎么做嗎?
Does anyone know what i should do?
謝謝.
推薦答案
此自定義代碼將保留免費(fèi)送貨方式,并在購物車金額達(dá)到250時隱藏其他送貨方式 并且如果產(chǎn)品不重(此處小于 20 公斤)……如果訂單少于 250 公斤,則不允許免費(fèi)送貨,您可以在 woocommerce 中進(jìn)行設(shè)置(見文末).
This custom code will keep free shipping method and will hide other shipping methods when the cart amount is up to 250 and if products are not heavy (less than 20 kg here)… To not allow Free shipping for orders less than 250, you can set this in woocommerce (see at the end).
首先,您必須確保在每個重型產(chǎn)品中設(shè)置了重量(對于簡單或可變產(chǎn)品(在每個變體中).這里的購物車小計是不含稅(并且您可以輕松地將其更改為包括稅金).
First you will have to sure that the weight is set in each heavy product (for simple or variables products (in each variations). The cart subtotal here is Excluding taxes (and you can change it to Including taxes easily).
然后是 woocommerce_package_rates
過濾器鉤子中的自定義鉤子函數(shù):
Then here is that custom hooked function in woocommerce_package_rates
filter hook:
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 文件或任何插件文件中.
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
此代碼經(jīng)過測試,適用于簡單和可變的產(chǎn)品......
This code is tested and it works for simple and variable products…
您還必須在 WooCommerce 設(shè)置中設(shè)置 >送貨,對于每個送貨區(qū)域和 免費(fèi)送貨"
方法,您的最低訂購量:
You Will also have to in WooCommerce settings > Shipping, for each shipping zone and for the
"Free Shipping"
method your minimum order amount:
您需要刷新運(yùn)輸緩存數(shù)據(jù):在woocommerce運(yùn)輸設(shè)置中禁用、保存和啟用、保存當(dāng)前運(yùn)輸區(qū)域的相關(guān)運(yùn)輸方式.
You will need to refresh shipping cached data: disable, save and enable, save related shipping methods for the current shipping zone, in woocommerce shipping settings.
這篇關(guān)于根據(jù)重量和最小購物車數(shù)量免費(fèi)送貨的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!