【问题标题】:Remove WooCommerce Payment Gateways for defined groups of product categories删除已定义产品类别组的 WooCommerce 支付网关
【发布时间】:2020-09-25 11:15:32
【问题描述】:

我已设法让此代码用于删除基于一个或多个产品类别的 ONE 支付网关。

我还需要删除多个支付网关。换一种说法;它应该根据一个或多个产品类别删除一个或多个支付网关。

这是我得到的,可能已经过时了?

add_filter( 'woocommerce_available_payment_gateways', 'remove_gateway_based_on_category' );
function remove_gateway_based_on_category($available_gateways){

    global $woocommerce;

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

        $unset = false;
        $category_ids = array( '75' );

    foreach ($woocommerce->cart->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['cod']);
    return $available_gateways;
}

【问题讨论】:

    标签: php wordpress woocommerce checkout payment-method


    【解决方案1】:

    是的,可以为多个产品类别的组禁用支付网关。

    1) 在下面的分离函数中,我们定义了我们的产品类别和支付网关组。产品类别可以是term(s) id(s)、slug(s) 或name(s)。所以在这个函数中我们定义了我们的设置,要使用:

    // The settings in a function
    function defined_categories_remove_payment_gateways() {
        // Below, Define by groups the categories that will removed specific defined payment gateways
        // The categories can be terms Ids, slugs or names
        return array(
            'group_1' => array(
                'categories'  => array( 11, 12, 16 ), // product category terms
                'payment_ids' => array( 'cod' ), // <== payment(s) gateway(s) to be removed
            ),
            'group_2' => array(
                'categories'  => array( 13, 17, 15 ), // product category terms
                'payment_ids' => array( 'bacs', 'cheque' ), // <== payment(s) gateway(s) to be removed
            ),
            'group_3' => array(
                'categories'  => array( 14, 19, 47 ),  // product category terms 
                'payment_ids' => array( 'paypal' ), // <== payment(s) gateway(s) to be removed
            ),
        );
    }
    

    2) 现在挂钩函数将在结帐页面中删除基于购物车项目产品类别的支付网关,加载我们的设置函数:

    add_filter( 'woocommerce_available_payment_gateways', 'remove_gateway_based_on_category' );
    function remove_gateway_based_on_category( $available_gateways ){
        // Only on checkout page
        if ( is_checkout() && ! is_wc_endpoint_url() ) {
            $settings_data  = defined_categories_remove_payment_gateways(); // Load settings
            $unset_gateways = []; // Initializing
    
            // 1. Loop through cart items
            foreach ( WC()->cart->get_cart() as $cart_item ) {
                // 2. Loop through category settings
                foreach ( $settings_data as $group_values ) {
                    // // Checking the item product category
                    if ( has_term( $group_values['categories'], 'product_cat', $cart_item['product_id'] ) ) {
                        // Add the payment gateways Ids to be removed to the array
                        $unset_gateways = array_merge( $unset_gateways, $group_values['payment_ids'] );
                        break; // Stop the loop
                    }
                }
            }
    
            // Check that array of payment Ids is not empty
            if ( count($unset_gateways) > 0 ) {
                // 3. Loop through payment gateways to be removed
                foreach ( array_unique($unset_gateways) as $payment_id ) {
                    if( isset($available_gateways[$payment_id]) ) {
                        // Remove the payment gateway
                        unset($available_gateways[$payment_id]); 
                    }
                }
            }
        }
        return $available_gateways;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 2017-03-02
      • 2017-08-05
      • 2014-06-06
      • 1970-01-01
      • 2018-11-09
      • 2013-08-11
      相关资源
      最近更新 更多