0

I am trying to get the currently selected payment gateway before the user finished checking out. I do this because I added some custom field when the user selects the 'bacs'payment method. But currently, my function returns the payment method that was selected when the checkout process starts, not the one that is currently selected. Here is the code I am using.

/**
 * Process the checkout
 */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    $selected_payment_method_id = WC()->session->get( 'chosen_payment_method' ); // this code returns cod even when bacs is selected because cod was selected when the checkout process started
    if($selected_payment_method_id == 'bacs'){
        // Check if set, if its not set add an error.
        if ( ! $_POST['extra_info_company'] || ! $_POST['extra_info_ordernumber'] || ! $_POST['extra_info_first_name'] || ! $_POST['extra_info_last_name'] || ! $_POST['extra_info_phonenumber'] || ! $_POST['extra_info_email'] )
            wc_add_notice( __( 'Vul alle extra velden in.' ), 'error' );
    }
}

Can anybody help me out?

Marty
  • 23
  • 2

2 Answers2

0

This is for anybody else that finds this. Sadly the way this function doesn't allow you to get the current payment method, the way I fixed it is by adding a hidden field that updates with the selected payment method using jQuery.

Marty
  • 23
  • 2
  • could you tell me exactly how you fixed it? I asked a similar question today: https://stackoverflow.com/questions/74795067/validation-of-conditional-checkbox-in-woocommerce – fredrik Dec 14 '22 at 10:32
  • Sadly I can't seem to find the code I used for this solution anymore. But I will try to explain it in some more detail. I needed to know the details of the selected payment method, but the function I used in the original question kept returning the previously selected one. So I added a hidden field to the checkout page, then using jQuery when the user selects their payment method I would write the name of the method in the hidden field. Then I could just read out that field to find out the correct payment method. I hope this helps you. – Marty Dec 15 '22 at 11:50
  • @Marty...the validation function here https://stackoverflow.com/questions/56253265/save-and-display-specific-payment-gateway-additional-field-everywhere-in-woocomm works, but unfortunately only for select and text fields, if I change the field type to checkbox the validation is again always executed unfortunately. Do you have an idea why it is working for text and select fields? – fredrik Dec 15 '22 at 15:43
0

You can totally get the payment method from the woocommerce_checkout_process hook. You just have to snag the [payment_method] from the $_POST like this:

add_action('woocommerce_checkout_process', 'blacklist_woocommerce_checkout_process');
function blacklist_woocommerce_checkout_process() {
    echo '<pre>'; print_r( $_POST ); echo '</pre>'; // view in the chrome DevTools console
    $payment_method = $_POST['payment_method'];
    if ( $payment_method == 'authorize_net_cim_credit_card' ) { // hard code for now
        // I do some stuff like throw an error if I want
    }
}

This is the $_POST output in my woocommerce_checkout_process hook (redacted, of course):

(
    [billing_first_name] => First
    [billing_last_name] => Last
    [billing_company] => Company
    [billing_country] => US
    [billing_address_1] => Street
    [billing_address_2] => 
    [billing_city] => City
    [billing_state] => ST
    [billing_postcode] => 00000
    [billing_phone] => (000) 000-0000
    [billing_email] => email@gmail.com
    [shipping_first_name] => First
    [shipping_last_name] => Last
    [shipping_company] => Company
    [shipping_country] => US
    [shipping_address_1] => Street
    [shipping_address_2] => 
    [shipping_city] => City
    [shipping_state] => ST
    [shipping_postcode] => 00000
    [order_comments] => 
    [shipping_method] => Array
        (
            [0] => flat_rate:1
        )

    [payment_method] => cod
    [wc-authorize-net-cim-credit-card-expiry] => 01 / 24
    [wc-authorize-net-cim-credit-card-payment-nonce] => 
    [wc-authorize-net-cim-credit-card-payment-descriptor] => 
    [wc-authorize-net-cim-credit-card-last-four] => 
    [wc-authorize-net-cim-credit-card-card-type] => 
    [woocommerce-process-checkout-nonce] => 2f8875d74e
    [_wp_http_referer] => /?wc-ajax=update_order_review
)
Scotty G
  • 374
  • 2
  • 6