Woocommerce Payment Method Detection in Checkout Page

十年热恋 提交于 2021-02-08 09:55:46

问题


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?


回答1:


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.

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 code:

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

});

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

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.

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');

This code can be placed in functions.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!



来源:https://stackoverflow.com/questions/42930063/woocommerce-payment-method-detection-in-checkout-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!