問題描述
我需要 woocommerce 運輸選項方面的幫助,我想隱藏特定產品類別的統一費率,我只想顯示本地交付或本地取貨選項.
對于所有其他類別,所有選項都應該有效.
我嘗試使用該代碼(添加到我的主題的 function.php 文件中):
function car_has_product_with_orange_cats() {全球 $woocommerce;$product_in_cart = 假;//開始獲取購物車項目的循環foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {$_product = $values['data'];$terms = get_the_terms( $_product->id, 'product_cat' );//二級循環搜索,以防某些項目有多個類別如果($條款){foreach ($terms as $term) {$_categoryid = $term->term_id;如果($_categoryid == 16){//類別在購物車中!$product_in_cart = 真;}}}}返回 $product_in_cart;}//添加過濾器和函數以隱藏方法add_filter('woocommerce_available_shipping_methods', 'custom_shipping_methods', 10, 1);函數 custom_shipping_methods( $available_methods ){如果(cart_has_product_with_orange_cats()){foreach($available_methods as $key => $method){if( $key == 'local_delivery' || $key == 'local_pickup'){繼續;}未設置($available_methods[$key]);}//刪除你想要的費率}//返回沒有您未設置的可用方法.返回 $available_methods;}
但它對我不起作用,可能是因為最新的 WooCommerce 版本或任何其他問題的代碼已過時.
我怎樣才能做到這一點?
謝謝.
更新 (兼容WC 3+,也適用于可變產品)
<塊引用>此答案的完整功能測試和更正代碼位于另一個線程上:
送貨方式 - 本地提貨隱藏統一費率時選項不可用
<小時><塊引用>
重要提示:woocommerce_available_shipping_methods
hook 已棄用,因為 WC 版本 2.1+,您需要改用 woocommerce_package_rates
過濾器鉤子.
WooCommerce 2.6+ 版引入了新的送貨區域.因此,所有與運費相關的 WooCommerce 先前版本代碼都過時,將不再起作用.
<塊引用>有關信息: 全球 $woocommerce;
** 與 $woocommerce->cart
已被 WC()->cart
取代.
您將使用 WC()->cart->get_cart()
...
我們將不再需要您的自定義條件函數,因為 WordPress 中已經存在一個條件函數,您可以將其定位為 WooCommerce 產品類別.此函數接受術語 ID、術語 slug 或術語名稱:
has_term( 'your_category', 'product_cat', $post_id )
所以我們可以在這段代碼中使用has_term()
條件函數:
add_filter( 'woocommerce_package_rates', 'conditional_hide_shipping_methods', 100, 2 );函數conditional_hide_shipping_methods( $rates, $package ){//在此處定義/替換您正確的類別 slug (!)$product_category = 'your_category';$prod_cat = 假;//瀏覽購物車中的每件商品,看看是否有屬于您的類別foreach ( WC()->cart->get_cart() 作為 $cart_item ) {$product_id = $cart_item['product_id'];if ( has_term( $product_category, 'product_cat', $product_id ) ){$prod_cat = 真;}}$rates_arr = array();如果($prod_cat){foreach($rates as $key => $rate) {if ('free_shipping' === $rate->method_id || 'local_pickup' === $rate->method_id || 'local_delivery' === $rate->method_id) {$rates_arr[ $rate_id ] = $rate;休息;}}}返回 !empty( $rates_arr ) ?$rates_arr : $rates;}
<塊引用>
在添加代碼段之前,請確保清除您的 WooCommerce 緩存(WooCommerce > 系統狀態 > 工具 > WC 瞬變 > 清除瞬變),因為運輸方式已被緩存.
此代碼位于活動子主題或主題的 function.php 文件中.
如果您有正確的設置您的送貨區域...
<小時>參考文獻:
- 隱藏其他運輸方式免費送貨時
- WooCommerce - 隱藏其他運輸免費送貨時的方法
- WooCommerce 2.6 -當達到特定金額觸發免費送貨時隱藏付費送貨
- WooCommerce 購物車 - 條件商品類別驗證莉>
- 如何在 WooCommerce 2.6 中設置送貨區域
I need help in woocommerce shipping options, I want to hide flat rate for a particular product category, where I only want to show local delivery or local pickup options.
For all others categories all options should work.
I have try to do it with that code (added in function.php file of my theme):
function cart_has_product_with_orange_cats() {
global $woocommerce;
$product_in_cart = false;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
// second level loop search, in case some items have several categories
if($terms){
foreach ($terms as $term) {
$_categoryid = $term->term_id;
if ( $_categoryid == 16 ) {
//category is in cart!
$product_in_cart = true;
}
}
}
}
return $product_in_cart;
}
// add filter and function to hide method
add_filter( 'woocommerce_available_shipping_methods', 'custom_shipping_methods' , 10, 1 );
function custom_shipping_methods( $available_methods ){
if ( cart_has_product_with_orange_cats() ) {
foreach($available_methods as $key => $method){
if( $key == 'local_delivery' || $key == 'local_pickup'){
continue;
}
unset($available_methods[$key]);
}
// remove the rate you want
}
// return the available methods without the one you unset.
return $available_methods;
}
But it isn't working for me, maybe because outdated code for latest WooCommerce version or any other issue.
How can I achieve this?
Thanks.
Update (Compatible with WC 3+ and works with variable products too)
The fully functional tested and corrected code for this answer is located on another thread:
Shipping methods - Local Pickup option not available when Flat Rate is hidden
Important:
woocommerce_available_shipping_methods
hook is deprecated since WC version 2.1+ and you will need to use insteadwoocommerce_package_rates
filter hook.
WooCommerce version 2.6+ introduce NEW shipping zones. So all WooCommerce prior version's code related to shipping rates is outdated and will not work anymore.
For info:
global $woocommerce;
** with$woocommerce->cart
has been replaced byWC()->cart
.
You will use insteadWC()->cart->get_cart()
…
We will not need anymore your custom conditional function, because there is already a conditional function that exist in WordPress, that you can target for WooCommerce product categories. This function accept the term ID, the term slug or the term name:
has_term( 'your_category', 'product_cat', $post_id )
So we can use has_term()
conditional function in this code:
add_filter( 'woocommerce_package_rates', 'conditional_hide_shipping_methods', 100, 2 );
function conditional_hide_shipping_methods( $rates, $package ){
// Define/replace here your correct category slug (!)
$product_category = 'your_category';
$prod_cat = false;
// Going through each item in cart to see if there is anyone of your category
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
if ( has_term( $product_category, 'product_cat', $product_id ) ){
$prod_cat = true;
}
}
$rates_arr = array();
if ( $prod_cat ) {
foreach($rates as $key => $rate) {
if ('free_shipping' === $rate->method_id || 'local_pickup' === $rate->method_id || 'local_delivery' === $rate->method_id) {
$rates_arr[ $rate_id ] = $rate;
break;
}
}
}
return !empty( $rates_arr ) ? $rates_arr : $rates;
}
Before adding the snippet, make sure you clear your WooCommerce cache (WooCommerce > System Status > Tools > WC Transients > Clear transients), as shipping methods are cached.
This code goes on function.php file of your active child theme or theme.
This code should work if you have correctly set your shipping zones…
References:
- Hide other shipping methods when FREE SHIPPING is available
- WooCommerce - Hide other shipping methods when FREE SHIPPING is available
- WooCommerce 2.6 - Hiding paid shipping when free shipping is triggered by reaching specific amount
- WooCommerce Cart - Conditional Items categories validation
- How to Setup Shipping Zones in WooCommerce 2.6
這篇關于在 WooCommerce 2.6 和 3+ 中刪除特定類別的運輸統一費率方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!