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

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

根據(jù) WooCommerce 結(jié)賬中的分類術(shù)語限制支付網(wǎng)關(guān)

根據(jù) WooCommerce 結(jié)賬中的分類術(shù)語限制支付網(wǎng)關(guān)

PHP
藍(lán)山帝景 2021-12-03 15:51:48
在我的 WooCommerce 商店中,只有當(dāng)產(chǎn)品具有類別 ID 為“266”的特定產(chǎn)品類別時(shí),我才想限制和顯示支付網(wǎng)關(guān)(支票)?,F(xiàn)在我有了這個(gè)片段,但它的作用正好相反——它禁用了特定產(chǎn)品類別結(jié)賬時(shí)的網(wǎng)關(guān):add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_unset_gateway_by_category' );  function bbloomer_unset_gateway_by_category( $available_gateways ) {    if ( is_admin() ) return $available_gateways;    if ( ! is_checkout() ) return $available_gateways;    $unset = false;    $category_ids = array( 8, 37 );    foreach ( WC()->cart->get_cart_contents() as $key => $values ) {        $terms = get_the_terms( $values['product_id'], 'product_cat' );            foreach ( $terms as $term ) {                    if ( in_array( $term->term_id, $category_ids ) ) {                $unset = true;                break;            }        }    }    if ( $unset == true ) unset( $available_gateways['cheque'] );    return $available_gateways;}
查看完整描述

2 回答

?
慕婉清6462132

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊

使用has_term()WordPress 條件函數(shù)將簡化代碼使其更有效,這樣:


add_filter( 'woocommerce_available_payment_gateways', 'filter_available_payment_gateways' );

function filter_available_payment_gateways( $available_gateways ) {

    // Only on checkout page

    if ( is_checkout() && ! is_wc_endpoint_url() ) {

        // Here define your product categories

        $product_categories = array( 't-shirts', 'posters' ); // Can be term names, term slugs or term ids

        $taxonomy = 'product_cat'; // For WooCommerce product category terms (or 'product_tag' for WooCommerce product tag terms)


        $payment_method     = 'cheque'; // Here define your payment method id to be removed

        $hide_payment       = false;


        // Loop through cart items

        foreach ( WC()->cart->get_cart_contents() as $item ) {

            if ( ! has_term( $product_categories, $taxonomy, $item['product_id'] ) ) {

                $hide_payment = true;

            }

        }

        

        if ( $hide_payment ) {

            unset( $available_gateways[$payment_method] );

        }

    }

    return $available_gateways;

}

代碼位于活動(dòng)子主題(或活動(dòng)主題)的 functions.php 文件中。測(cè)試和工作。


改為處理產(chǎn)品標(biāo)簽

只需將代碼中的分類法替換'product_cat'為'product_tag'.


也針對(duì)父產(chǎn)品類別

如果您還需要定位父產(chǎn)品類別,則必須改用它:


// Custom conditional function that handle parent product categories too

function has_product_categories( $categories, $product_id = 0 ) {

    $parent_term_ids = $categories_ids = array(); // Initializing

    $taxonomy        = 'product_cat';

    $product_id      = $product_id == 0 ? get_the_id() : $product_id;


    if( is_string( $categories ) ) {

        $categories = (array) $categories; // Convert string to array

    }


    // Convert categories term names and slugs to categories term ids

    foreach ( $categories as $category ){

        $result = (array) term_exists( $category, $taxonomy );

        if ( ! empty( $result ) ) {

            $categories_ids[] = reset($result);

        }

    }


    // Loop through the current product category terms to get only parent main category term

    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){

        if( $term->parent > 0 ){

            $parent_term_ids[] = $term->parent; // Set the parent product category

            $parent_term_ids[] = $term->term_id; // (and the child)

        } else {

            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.

        }

    }

    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;

}


add_filter( 'woocommerce_available_payment_gateways', 'filter_available_payment_gateways' );

function filter_available_payment_gateways( $available_gateways ) {

    // Only on checkout page

    if ( is_checkout() && ! is_wc_endpoint_url() ) {

        // Here define your product categories

        $product_categories = array( 't-shirts', 'posters' ); // Can be term names, term slugs or term ids

        $taxonomy = 'product_cat'; // For WooCommerce product category terms (or 'product_tag' for WooCommerce product tag terms)


        $payment_method     = 'cheque'; // Here define your payment method id to be removed

        $hide_payment       = false;


        // Loop through cart items

        foreach ( WC()->cart->get_cart_contents() as $item ) {

            if ( ! has_product_categories( $product_categories, $item['product_id'] ) ) {

                $hide_payment = true;

            }

        }

        

        if ( $hide_payment ) {

            unset( $available_gateways[$payment_method] );

        }

    }

    return $available_gateways;

}

代碼位于活動(dòng)子主題(或活動(dòng)主題)的 functions.php 文件中。測(cè)試和工作。


查看完整回答
反對(duì) 回復(fù) 2021-12-03
?
郎朗坤

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊

你能在你的代碼中改變你的條件嗎


if ( $unset == true ){

     $index = 0;

    foreach($available_gateways as $single){

        if($single != "cheque"){

            unset($available_gateways[$index]);

        }   

        $index++;

    }

 }


查看完整回答
反對(duì) 回復(fù) 2021-12-03
  • 2 回答
  • 0 關(guān)注
  • 227 瀏覽

添加回答

舉報(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)