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

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

如何根據項目數量和特定類別禁用送貨方式

如何根據項目數量和特定類別禁用送貨方式

PHP
拉莫斯之舞 2023-03-04 16:25:13
我一直在尋找一種有條件地禁用兩種運輸方式的方法表率距離率基于項目計數。我所說的項目計數不是指數量,而是指購物車中有多少種不同的產品。IE 2 Lamps and 3 tables in the cart would be an item count of 2 and a combined quantity of 5.我還想確保此規則僅對特定類別有效。我試過:function hide_shipping_count_based( $rates, $package ) {    // Set count variable    $cart_count = 0;    // Calculate cart's total    foreach( WC()->cart->cart_contents as $key => $value) {        $cart_count ++;    }    // only if the weight is over 150lbs find / remove specific carrier    if( $cart_count > 2 ) {        // loop through all of the available rates        unset( $rates[ 'distance_rate' ] );        unset( $rates[ 'table_rate' ] );    }    return $rates;}add_filter( 'woocommerce_package_rates', 'hide_shipping_count_based', 10, 2 );
查看完整描述

1 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

您可以使用以下解釋,并在代碼中添加注釋

此代碼必須滿足的條件是:

  • 購物車中至少有 3 個訂單項

  • 1 個或多個產品屬于“類別 1”類別

  • 如果滿足前兩個條件,“distance_rate”和/或“table_rate”將取消設置

function hide_shipping_count_based( $rates, $package ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;


    // Count line items

    $count =  count( $package['contents'] );


    // Set variable

    $found = false;


    // Set term (category)

    $term = 'categorie-1';


    // Check count

    if( $count > 2 ) {


        // Loop through line items

        foreach( $package['contents'] as $line_item ) {

            // Get product id

            $product_id = $line_item['product_id'];


            // Check for category

            if ( has_term( $term, 'product_cat', $product_id ) ) {

                $found = true;

                break;

            }

        }

    }


    // True

    if ( $found ) {

        // Loop trough rates

        foreach ( $rates as $rate_key => $rate ) {

            // Targeting

            if ( in_array( $rate->method_id, array( 'distance_rate', 'table_rate' ) ) ) {

                unset( $rates[$rate_key] );

            }

        }

    }


    return $rates;

}

add_filter( 'woocommerce_package_rates', 'hide_shipping_count_based', 100, 2 );


查看完整回答
反對 回復 2023-03-04
  • 1 回答
  • 0 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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