亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果購物車有超過 4 件特定運輸類別的商品,則隱藏一些運輸方式

如果購物車有超過 4 件特定運輸類別的商品,則隱藏一些運輸方式

PHP
鳳凰求蠱 2023-10-21 10:11:00
這是場景:我使用 UPS 運送飲料托盤。然而,將5盤飲料裝進盒子里就變得非常困難。因此,我想禁用 UPS 運輸方式,并且僅在客戶訂購 5 盤或以上飲料時才顯示統一費率運輸。我有大約 7 種不同的飲料,但我可以將這些飲料添加到運輸類別中以簡化代碼。我想擴展此代碼以包含特定類別中的產品數量,或者可能是運輸類別中的產品出現的次數。因此,如果購物車有 5 個或更多此特定運輸類別的產品,則應刪除我在數組下指定的運輸方式。如何擴展此代碼以包括產品數量?// 如果我們發現運輸類別和運輸類別中的產品數量等于或大于 5。add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );function hide_shipping_method_based_on_shipping_class( $rates, $package ){    if ( is_admin() && ! defined( 'DOING_AJAX' ) )        return;    // Shipping Class To Find    $class = 182;    // Number Of Shipping Class Items In Cart    $amount = 5;    // Shipping Methods To Hide    $method_key_ids = array('wf_shipping_ups:07', 'wf_shipping_ups:08', 'wf_shipping_ups:11', 'wf_shipping_ups:54', 'wf_shipping_ups:65', 'wf_shipping_ups:70', 'wf_shipping_ups:74', 'free_shipping:2', 'request_shipping_quote');編輯-添加:由于我從 PluginHive 購買了“帶有打印標簽的 WooCommerce UPS 運輸插件”,因此我可以訪問他們的“管理運輸方式”插件,該插件允許我執行以下操作:設置多個規則以從各種運輸類別中排除各種運輸方式。在第一次出現時中斷序列。我設置的規則如下:對于 150 類(首次出現時中斷)- 未設置:wf_shipping_ups:07、wf_shipping_ups:08、wf_shipping_ups:11、wf_shipping_ups:54、wf_shipping_ups:65、wf_shipping_ups:70、wf_shipping_ups:74、free_shipping:2、request_shipping_quote。對于 151 類 - 未設置:flat_rate:20、flat_rate:21。我在上面的代碼中為我想要定位的產品創建了第三個類 182。僅當添加到購物車的該類別商品少于 5 件時,才應將其視為類別 151。但如果購物車中添加 5 件或更多商品,則應將其視為 150 類。這就是我的困境。潛在的解決方案 - 添加:我想出了如何解決我的問題。如果購物車中的產品數量為 5 件或更多,@LoicTheAztec 幫助我的代碼可以讓我取消給定運輸類別的運輸方式。我現在需要做的是取消設置其他兩種運輸方式(flat_rate:20 和 flat_rate:21),這兩種運輸方式會導致沖突,對于相同的運輸類別(182),但這次購物車中的產品數量為 4 或更少( =<)。然后我可以使用現有的插件來創建以下規則:第一次出現時中斷(檢查)對于 150 級 - 未設置:wf_shipping_ups:07、wf_shipping_ups:08、wf_shipping_ups:11、wf_shipping_ups:54、wf_shipping_ups:65、wf_shipping_ups:70、wf_shipping_ups:74、free_shipping:2、request_shipping_quote。對于 182 類 - 未設置:沒什么 - 因為兩個代碼都會創建邏輯對于 151 類 - 未設置:扁平率:20,扁平率:21。這應該可以解決由插件引起的沖突。百萬美元的問題是......我可以以某種方式使用@LoicTheAztec 的解決方案來設置某種最小數量嗎?
查看完整描述

1 回答

?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

如果特定運輸類別的總物品數為 5 或更多,以下內容將隱藏特定定義的運輸方式:


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

function hide_shipping_method_based_on_shipping_class( $rates, $package ) {

    $targeted_class_ids = array(182); // Shipping Class To Find

    $allowed_max_qty    = 4; // Max allowed quantity for the shipping class

    $shipping_rates_ids = array( // Shipping Method rates Ids To Hide

        'wf_shipping_ups:07',

        'wf_shipping_ups:08',

        'wf_shipping_ups:11',

        'wf_shipping_ups:54',

        'wf_shipping_ups:65',

        'wf_shipping_ups:70',

        'wf_shipping_ups:74',

        'free_shipping:2',

        'request_shipping_quote'

    );

    

    $related_total_qty  = 0;


    // Checking cart items for current package

    foreach( $package['contents'] as $key => $cart_item ) {

        if( in_array( $cart_item['data']->get_shipping_class_id(), $targeted_class_ids ) ){

            $related_total_qty += $cart_item['quantity'];

        }

    }

    

    // When total allowed quantity is more than allowed (for items from defined shipping classes)

    if ( $related_total_qty > $allowed_max_qty ) {

        // Hide related defined shipping methods

        foreach( $shipping_rates_ids as $shipping_rate_id ) {

            if( isset($rates[$shipping_rate_id]) ) {

                unset($rates[$shipping_rate_id]); // Remove Targeted Methods

            }

        }

    }

代碼位于活動子主題(或活動主題)的functions.php 文件中。未經測試它應該有效。

刷新運輸緩存:

  1. 此代碼已保存在您的functions.php 文件中。

  2. 在運輸區域設置中,禁用/保存任何運輸方式,然后啟用返回/保存。

    你已經完成了,你可以測試它。


處理物品數量而不是物品累計數量:

代替:

$related_total_qty += $cart_item['quantity'];

經過

$related_total_qty++;


查看完整回答
反對 回復 2023-10-21
  • 1 回答
  • 0 關注
  • 133 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號