久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 WooCommerce 2.6 和 3+ 中刪除特定類別的運輸統一

Remove shipping Flat Rate method for particular Category in WooCommerce 2.6 and 3+(在 WooCommerce 2.6 和 3+ 中刪除特定類別的運輸統一費率方法)
本文介紹了在 WooCommerce 2.6 和 3+ 中刪除特定類別的運輸統一費率方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要 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 instead woocommerce_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 by WC()->cart.
You will use instead WC()->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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 免费看国产片在线观看 | 久久久久亚洲 | 国产一区视频在线 | 日韩欧美在线一区 | 黄网站免费在线观看 | 国产成人综合一区二区三区 | 男人天堂av网站 | 日韩av一区二区在线观看 | 国产粉嫩尤物极品99综合精品 | 国产欧美视频一区 | 久久日韩精品一区二区三区 | 日本网站免费在线观看 | 91久久国产综合久久91精品网站 | 国产伦精品一区二区三区在线 | 国产黑丝av | 99视频免费在线观看 | 91视频在线 | 69xxx免费| 99精品免费视频 | 久久网站免费视频 | 国产一区二区在线视频 | 欧美日韩在线观看一区二区三区 | 久久躁日日躁aaaaxxxx | 三级黄色片在线 | 精品国偷自产在线 | 久久久久国产一区二区三区四区 | 日本又色又爽又黄的大片 | 啪啪综合网 | 91精品久久| 日韩成人免费视频 | 国产成人综合一区二区三区 | 欧美精品日韩精品 | 夜操| 蜜桃精品视频在线 | 亚洲精品www | 91免费观看视频 | www.日韩在线 | www.亚洲 | 日韩精品极品视频在线观看免费 | 人人干视频在线 | 九九亚洲 |