根据WooCommerce结帐中的分类条款限制支付网关 [英] Restrict payment gateways based on taxonomy terms in WooCommerce checkout

查看:79
本文介绍了根据WooCommerce结帐中的分类条款限制支付网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WooCommerce商店中,仅当产品具有类别ID为"266"的特定产品类别时,我才想限制并显示付款网关(支票).现在,我有了这个代码段,但它却相反-它在特定产品类别的结帐时禁用了网关:

In my WooCommerce store I want to restrict and show payment gateway(cheque) only if the product has particular product category with the category ID "266". Now I have this snippet but it does the opposite - it disabled the gateway on the checkout for particular product category:

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

推荐答案

使用 has_term() WordPress条件函数,它将通过以下方式简化代码,使其更有效:

Using has_term() WordPress conditional function that will simplify the code making it more effective, this way:

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

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

只需在代码中将分类法'product_cat'替换为'product_tag'.


也定位父产品类别

如果您还需要定位父产品类别,则您将不得不使用它:


Targeting parent product category too

If you need to target the parent product categories too, you will have to use this instead:

// 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;
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

这篇关于根据WooCommerce结帐中的分类条款限制支付网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆