第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何根據(jù)項(xiàng)目數(shù)量和特定類別禁用送貨方式

如何根據(jù)項(xiàng)目數(shù)量和特定類別禁用送貨方式

PHP
拉莫斯之舞 2023-03-04 16:25:13
我一直在尋找一種有條件地禁用兩種運(yùn)輸方式的方法表率距離率基于項(xiàng)目計(jì)數(shù)。我所說的項(xiàng)目計(jì)數(shù)不是指數(shù)量,而是指購物車中有多少種不同的產(chǎn)品。IE 2 Lamps and 3 tables in the cart would be an item count of 2 and a combined quantity of 5.我還想確保此規(guī)則僅對(duì)特定類別有效。我試過: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貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊

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

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

  • 購物車中至少有 3 個(gè)訂單項(xiàng)

  • 1 個(gè)或多個(gè)產(chǎn)品屬于“類別 1”類別

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

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 );


查看完整回答
反對(duì) 回復(fù) 2023-03-04
  • 1 回答
  • 0 關(guān)注
  • 134 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)