【问题标题】:Show/Hide payment methods for various product categories in WooCommerce在 WooCommerce 中显示/隐藏各种产品类别的付款方式
【发布时间】:2020-03-28 17:58:00
【问题描述】:

所以,我看到了如何根据产品类别取消设置支付网关的示例,例如这个(效果很好):

add_filter( 'woocommerce_available_payment_gateways', 
'unset_gateway_by_category' );

function unset_gateway_by_category( $available_gateways ) {
    if ( is_admin() ) return $available_gateways;
    if ( ! is_checkout() ) return $available_gateways;
    $unset = false;
    $category_ids = array( 11810 );
    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['worldpay'] );
    if ( $unset == true ) unset( $available_gateways['ppec_paypal'] );
    return $available_gateways;
    }

在这种情况下,如果购物车中有任何产品类别 ID 为 11810 的产品,则删除付款方式 Worldpay 和 PayPal。

但是,它并不能完全满足我的需求。

所以,如果您有类别 1、2、3 和 4...以及付款方式 a、b 和 c。

产品类别4只能使用支付网关c,产品类别1,2和3只能使用支付网关a和b。

如果购物车中有所有类别的产品(包括类别 4),那么上面的代码将隐藏支付方式 a 和 b,只留下网关 c(完美运行)。但是...如果购物车包含任何类别 4 的产品,如何取消设置网关 c?

我想我需要一个 elsif 在某个地方,但是...我不是 php ninja,所以希望能得到一些指导。

提前致谢。

更新:

下面的版本是迄今为止最接近的。

当购物车中没有受限产品时,它会隐藏专家网关。 当购物车中只有受限产品时,它会隐藏常规网关。

但是...如果有一个普通产品和受限产品的麦克风...它会隐藏所有网关。

add_filter( 'woocommerce_available_payment_gateways', 
'unset_gateway_by_category' );

function unset_gateway_by_category( $available_gateways ) {
if ( is_admin() ) return $available_gateways;
if ( ! is_checkout() ) return $available_gateways;
$unset = false;
$category_ids = array( 15, 166, 11810 );

/*This will dynamically map the payment gateway that need to be hidden for 
the specific category id*/
$category_payment_map = array(
    '15' =>  array('totalprocessing'),
    '166' =>  array('totalprocessing'),
    '11810' =>  array('worldpay', 'ppec_paypal')

    );

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 ) ) {
                /*Based on Term Id, fetch the payment gateway that need to 
be hide from the $category_payment_map array*/
                $payment_gateway_ids =  $category_payment_map[$term->term_id];

                /*you can check here whether the $payment_gateway_id is an array or sin*/
                foreach ($payment_gateway_ids as $key => $payment_gateway_id) {
                    unset( $available_gateways[$payment_gateway_id] );
                }

            }
        }
    }
    return $available_gateways;
    }

我认为它需要某种规则来说明,如果购物车混合了常规产品和受限产品,则受限产品规则适用并覆盖其他规则?

谢谢

【问题讨论】:

  • $payment_gateway_id替换拼写错误$$payment_gateway_id
  • 修复了这个问题。谢谢。但是,一切都很好,直到您的购物车中混合了常规产品和受限产品。在这种情况下,它会取消设置所有支付网关。
  • 很高兴听到它运行良好。谢谢
  • @MominIqbal 不,你误会了,它工作不正常。如果您的购物车中有常规产品和受限产品,它会隐藏所有网关。

标签: php wordpress woocommerce


【解决方案1】:

是的,您的代码中有一些错误的逻辑表示来取消设置支付网关。我已经修改了代码并提供了对修改后代码的评论。

请应用代码并让我知道它是否有效。 希望您的问题能得到肯定的解决

    add_filter( 'woocommerce_available_payment_gateways', 'unset_gateway_by_category' );

    function unset_gateway_by_category( $available_gateways ) {
    if ( is_admin() ) return $available_gateways;
    if ( ! is_checkout() ) return $available_gateways;
    $unset = false;
    $category_ids = array( 11810, 11900 );

    /*This will dynamically map the payment gateway that need to be hide for the specific category id*/
    $category_payment_map = array(
                                   '11810' =>  array('worldpay', 'ppec_paypal'),
                                   '11900' =>  array('ppec_paypal')
                                );

    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 ) ) {
                /*Based on Term Id, fetch the payment gateway that need to be hide from the $category_payment_map array*/
                $payment_gateway_ids =  $category_payment_map[$term->term_id];

                /*you can check here whether the $payment_gateway_id is an array or sin*/
                foreach ($payment_gateway_ids as $key => $payment_gateway_id) {
                    unset( $available_gateways[$payment_gateway_id] );
                }

            }
        }
    }
    return $available_gateways;
    }

【讨论】:

  • 这已经在正确的道路上,但如果你检查你的代码,最后你只是返回 $available_gateways 而没有任何改变......
  • @Reigel 实际上我的代码中有一个错字,我现在已经更新了,谢谢指出。
  • 感谢您的回复。 @MominIqbal 在您的示例中有点困惑“11900”的来源?在我对用例的解释中,可能是我的错。试图澄清。这家商店的使用案例,他们有一些 CBD 产品,他们必须通过专业支付网关,所以... 只是常规产品 = 常规支付网关(未设置专业网关) 只是 CBD 产品,混合常规和 CBD 产品的数量 = 专业网关(未设置常规网关)。这是否说明了我想要实现的目标?
  • @MominIqbal 哦,等等...我想我明白了... $category_payment_map = array( '11810' => 'worldpay', '11900' => 'ppec_paypal' );那么,我会将所有产品类别 ID 放在这里,每个组后面都是我不想为该组类别显示的付款方式?
  • 对于特定的分类,如果你想隐藏多个支付网关,那么你可以使用数组格式。请参阅我的更新答案
猜你喜欢
  • 2018-03-01
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 2020-08-25
  • 2018-11-09
  • 2016-06-11
  • 1970-01-01
相关资源
最近更新 更多