结帐页面中的 Woocommerce 付款方式检测 [英] Woocommerce Payment Method Detection in Checkout Page

查看:62
本文介绍了结帐页面中的 Woocommerce 付款方式检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 woocommerce 插件和 woocommerce 的braintree 扩展进行支付.我已经启用了 woocommerce Braintree 的卡和贝宝支付来结帐.我试图弄清楚如何在用户实际结账和支付之前知道用户选择了哪个支付网关.在woocommercebraintree 下查找信用卡单选按钮或paypal 付款单选按钮的任何钩子 都会被检查是否付款.

I am using woocommerce plugin and braintree extension of woocommerce for payment. I have enabled both card and paypal payment of woocommerce braintree to checkout. I am trying to figure out how to know which payment gateway the user selects before user actually checkouts and pays. Any hooks under woocommerce or braintree to find either credit card radio button or paypal payment radio button is checked for payment.

但是我知道我们可以在成功付款后检测用于特定订单的网关,但我希望在结帐页面内付款完成之前选择网关信息.有什么帮助吗?

However i know we can detect the gateway used for the particular order after successful payment but i want the selected gateway information before payment completes within checkout page. Any Help?

推荐答案

您可以在结帐页面上使用一些基本的 JavaScript 检测选择的付款方式,并通过挂钩到 woocommerce_checkout_update_order_review 行动.

You can detect chosen Payment Method with some basic JavaScript on checkout page and run your custom code with PHP by hooking into woocommerce_checkout_update_order_review action.

首先,您还应该在结帐页面、结帐模板或主题的页眉/页脚中放置 JS 代码,以便您可以检测用户何时更改了付款方式选项并在此之后运行您自己的代码.

First, you also should place JS code on checkout page, checkout template or in header/footer of your theme, so you can detect when the user has changed payment method option and run your own code after that.

JS代码:

jQuery(document).ready( function() {

    jQuery( "#payment_method_bacs" ).on( "click", function() {
        jQuery( 'body' ).trigger( 'update_checkout' );
    });

    jQuery( "#payment_method_paypal" ).on( "click", function() {
        jQuery(document.body).trigger("update_checkout");
    });

    jQuery( "#payment_method_stripe" ).on( "click", function() {
        jQuery(document.body).trigger("update_checkout");
    });

});

请注意,对于您激活的每种付款方式,您都应该添加点击"事件.它为您提供了在触发自定义代码时进行微调的选项.为了防止点击事件只运行一次,您应该在第一个下面添加下一个 JS 代码块.

Notice that for each payment method you have active you should add 'Click' event. It gives you option to fine tune when your custom code is triggered. To prevent click event to run ONLY ONCE you should add next block of JS code below first one.

jQuery( document ).ajaxStop(function() {

    jQuery( "#payment_method_bacs" ).on( "click", function() {
        jQuery(document.body).trigger("update_checkout");
    });

    jQuery( "#payment_method_paypal" ).on( "click", function() {
        jQuery(document.body).trigger("update_checkout");
    });

    jQuery( "#payment_method_stripe" ).on( "click", function() {
        jQuery(document.body).trigger("update_checkout");
    });
});

只是在ajax之后触发的代码相同.在两个 JS 代码块中添加您实际使用的付款选项.

It's the same code only that is triggered after ajax. In both JS blocks of code add your payment options that you are actually use.

之后,您将自定义的 PHP 代码放入结帐中,如下所示:

After that you put your custom PHP code that hooks into checkout like this:

if ( ! function_exists( 'name_of_your_function' ) ) :
    function name_of_your_function( $posted_data) {

        // Your code goes here

     }

endif;

add_action('woocommerce_checkout_update_order_review', 'name_of_your_function');

这段代码可以放在functions.php中.

This code can be placed in functions.php.

以下是完整的 PHP 代码,用于在结帐页面上选择特定付款选项时检测并运行:

Here is complete PHP code that detects and run when specific payment option is chosen on checkout page:

function name_of_your_function( $posted_data) {

    global $woocommerce;

    // Parsing posted data on checkout
    $post = array();
    $vars = explode('&', $posted_data);
    foreach ($vars as $k => $value){
        $v = explode('=', urldecode($value));
        $post[$v[0]] = $v[1];
    }

    // Here we collect payment method
    $payment_method = $post['payment_method'];

    // Run code custom code for each specific payment option selected
    if ($payment_method == "paypal") {
        // Your code goes here
    }

    elseif ($payment_method == "bacs") {
        // Your code goes here
    }

    elseif ($payment_method == "stripe") {
        // Your code goes here
    }
}

add_action('woocommerce_checkout_update_order_review', 'name_of_your_function');

我希望这会有所帮助!这是在结帐页面上运行所有自定义逻辑的非常强大的选项!

I hope this helps! This is a very powerful option to run all your custom logic on the checkout page!

这篇关于结帐页面中的 Woocommerce 付款方式检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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