【问题标题】:Restrict payment gateways based on specific product tag in WooCommerce order pay根据 WooCommerce 订单支付中的特定产品标签限制支付网关
【发布时间】:2021-04-27 00:15:27
【问题描述】:

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

例如如果标签的 id 是“378”,则只显示“PayPal”网关并删除其他网关。

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

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

Woocommerce Bookings 支付页面中如何根据产品标签限制支付网关?

【问题讨论】:

    标签: php wordpress woocommerce payment-gateway taxonomy-terms


    【解决方案1】:

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

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

    代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

    见:Conditional Tags in WooCommerce

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

    【讨论】:

    • 谢谢 Loic - 我尝试添加 if ( is_checkout() || is_wc_endpoint_url( 'order-pay' ) ) { 但它仍然在 woocommerce 订单支付页面上显示所有网关..
    • 谢谢洛伊克!真的很感激。
    猜你喜欢
    • 2017-07-23
    • 1970-01-01
    • 2019-12-28
    • 2016-12-09
    • 2021-01-07
    • 1970-01-01
    • 2020-09-25
    • 2017-03-02
    • 2017-08-05
    相关资源
    最近更新 更多