在WooCommerce订单支付中限制基于特定产品标签的支付网关 [英] Restrict payment gateways based on specific product tag in WooCommerce order pay

查看:25
本文介绍了在WooCommerce订单支付中限制基于特定产品标签的支付网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据产品标签在WooCommerce Bookings支付页面上显示一个支付网关,该网关位于URL扩展名上,如下所示:
/checkout/order-pay/5759158/?pay_for_order=true&key=wc_order_75uA3d1z1fmCT

例如,如果标记的ID是&q;378&q;,则仅显示&q;PayPal&q;网关并删除其他网关。

我使用的是Restrict payment gateways based on taxonomy terms in WooCommerce checkout答案代码,它允许根据产品标签限制支付网关,但仅限于WooCommerce结账页面。

我还需要在WooCommerce预订付款页面上对其进行限制。

如何在WooCommerce预订支付页面中根据产品标签限制支付网关?

推荐答案

对于订单支付页面,您需要遍历订单项目而不是购物车项目,以检查产品标签术语…要定位订单支付页面,请使用:

if ( is_wc_endpoint_url( 'order-pay' ) ) {

以下代码将禁用除";PayPal";以外的所有付款方式,当订单付款页面(以及结账)中存在属于特定产品标签条款的商品时:

add_filter( 'woocommerce_available_payment_gateways', 'filter_available_payment_gateways' );
function filter_available_payment_gateways( $available_gateways ) {
    // Here below your settings
    $taxonomy    = 'product_tag'; // Targeting WooCommerce product tag terms (or "product_cat" for category terms)
    $terms       = array('378'); // Here define the terms (can be term names, slugs or ids)
    $payment_ids = array('paypal'); // Here define the allowed payment methods ids to keep
    $found       = false; // Initializing

    // 1. For Checkout page
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $item ) {
            if ( ! has_term( $terms, $taxonomy, $item['product_id'] ) ) {
                $found = true;
                break;
            }
        }
    }
    // 2. For Order pay
    elseif ( is_wc_endpoint_url( 'order-pay' ) ) {
        global $wp;

        // Get WC_Order Object from the order id
        $order = wc_get_order( absint($wp->query_vars['order-pay']) );

        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            if ( ! has_term( $terms, $taxonomy, $item->get_product_id() ) ) {
                $found = true;
                break;
            }
        }
    }

    if ( $found ) {
        foreach ( $available_gateways as $payment_id => $available_gateway ) {
            if ( ! in_array($payment_id, $payment_ids) ) {
                unset($available_gateways[$payment_id]);
            }
        }
    }
    return $available_gateways;
}

代码放在活动子主题(或活动主题)的函数.php文件中。它应该可以工作。

参见:Conditional Tags in WooCommerce

相关:Restrict payment gateways based on taxonomy terms in WooCommerce checkout

这篇关于在WooCommerce订单支付中限制基于特定产品标签的支付网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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