简体   繁体   English

根据 WooCommerce 订单支付中的特定产品标签限制支付网关

[英]Restrict payment gateways based on specific product tag in WooCommerce order pay

I want to show a payment gateway on the Woocommerce Bookings payment page based on the product's tag and located on the URL extension like:我想根据产品标签在 Woocommerce 预订支付页面上显示支付网关,该支付网关位于 URL 扩展上,例如:
/checkout/order-pay/5759158/?pay_for_order=true&key=wc_order_75uA3d1z1fmCT

Eg if the tag's id is "378", then show only "PayPal" gateway and remove other gateways.例如,如果标签的 id 是“378”,则只显示“PayPal”网关并删除其他网关。

I am using Restrict payment gateways based on taxonomy terms in WooCommerce checkout answer code, that allows to restricts the payment gateway based on a product tag, but only on the Woocommerce checkout page.在 WooCommerce 结帐答案代码中使用基于分类术语的限制支付网关,它允许基于产品标签限制支付网关,但仅在 Woocommerce 结帐页面上。

I need to restrict it on the Woocommerce Bookings payment page as well.我还需要在 Woocommerce 预订付款页面上对其进行限制。

How to restrict the payment gateway based on a product tag in Woocommerce Bookings payment page? Woocommerce Bookings 支付页面如何根据产品标签限制支付网关?

For order pay pages, you need to loop through order items instead of cart items, to check for product tag terms… To target order pay page use:对于订单支付页面,您需要遍历订单项目而不是购物车项目,以检查产品标签条款……要定位订单支付页面,请使用:

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

The following code will disable all payment methods except "paypal" when there is an item that belongs to specific product tag term(s) in order pay page (and checkout too) :当订单支付页面(以及结帐)中存在属于特定产品标签术语的商品时,以下代码将禁用除“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;
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 It should work.它应该工作。

See: Conditional Tags in WooCommerce参见: WooCommerce 中的条件标签

Related: Restrict payment gateways based on taxonomy terms in WooCommerce checkout相关: 根据 WooCommerce 结帐中的分类术语限制支付网关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 WooCommerce 结账中的分类术语限制支付网关 - Restrict payment gateways based on taxonomy terms in WooCommerce checkout 删除已定义产品类别组的 WooCommerce 支付网关 - Remove WooCommerce Payment Gateways for defined groups of product categories 根据产品标签更改Woocommerce单一产品模板的订单 - Change Woocommerce single product templates order based on product tag 根据 Woocommerce 中的购物车总金额添加或删除支付网关 - add or remove payment gateways based on cart total amount in Woocommerce 根据 Woocommerce 中的延期交货项目显示隐藏支付网关 - Show hide payment gateways based on backordered items in Woocommerce 根据 WooCommerce 中的“标签 ID”在单个产品页面上添加特定信息 - Add specific info on single product page based on "Tag ID" in WooCommerce WooCommerce - 多种支付方式同时支付订单 - WooCommerce - pay order with multiple payment methods at same time woocommerce产品如何通过产品标签获得订单 - how woocommerce product get order by product tag 如果购物车中有特定产品,请删除付款网关(Woocommerce) - Remove payment gateway if specific product is in the cart (Woocommerce) WooCommerce 禁止特定产品类别的 ClearPay 付款 - WooCommerce Disallow ClearPay Payment for specific product categories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM