【问题标题】:WooCommerce order-received redirect based on payment method基于付款方式的 WooCommerce 订单接收重定向
【发布时间】:2018-02-13 18:11:29
【问题描述】:

通常在 WooCommerce 中提交的订单会在付款完成后重定向到 /order-received/

是否可以将客户重定向到特定付款方式的自定义页面?

例如:

Payment method 1 -> /order-received/
Payment method 2 -> /custom-page/
Payment method 3 -> /order-received/

【问题讨论】:

    标签: php wordpress woocommerce orders payment-method


    【解决方案1】:

    通过使用条件函数 is_wc_endpoint_url()template_redirect 操作钩子中挂钩的自定义函数,并定位您想要的付款方式以将客户重定向到特定页面:

    add_action( 'template_redirect', 'thankyou_custom_payment_redirect');
    function thankyou_custom_payment_redirect(){
        if ( is_wc_endpoint_url( 'order-received' ) ) {
            global $wp;
    
            // Get the order ID
            $order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
    
            // Get an instance of the WC_Order object
            $order = wc_get_order( $order_id );
    
            // Set HERE your Payment Gateway ID
            if( $order->get_payment_method() == 'cheque' ){
                
                // Set HERE your custom URL path
                wp_redirect( home_url( '/custom-page/' ) );
                exit(); // always exit
            }
        }
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    此代码已经过测试并且可以工作。

    如何获取支付网关 ID(WC 设置 > Checkout 标签):

    【讨论】:

    • 您的操作将无法正常工作,因为您总是从template_redirect退出,您需要在重定向到另一个页面wp_redirect 后退出。
    【解决方案2】:

    一个小的修正。

    “退出”需要在最后一个条件内

    add_action( 'template_redirect', 'thankyou_custom_payment_redirect');
        function thankyou_custom_payment_redirect(){
        if ( is_wc_endpoint_url( 'order-received' ) ) {
            global $wp;
    
            // Get the order ID
            $order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
    
            // Get an instance of the WC_Order object
            $order = wc_get_order( $order_id );
    
            // Set HERE your Payment Gateway ID
            if( $order->get_payment_method() == 'cheque' ){
    
                // Set HERE your custom URL path
                wp_redirect( home_url( '/custom-page/' ) );
                exit(); // always exit
            }
        }
    }
    

    【讨论】:

    • 有兴趣知道为什么我的修正案在没有评论的情况下被否决,因为原始解决方案将在第二次操作时在整个 Woocommerce 安装中产生 500 错误。
    猜你喜欢
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 2017-04-07
    • 2019-07-26
    • 2017-05-17
    • 1970-01-01
    • 2022-10-26
    相关资源
    最近更新 更多