Overwrite payment receiver if specific payment option selected ( woocommerce / php )

I’m really getting headaches over this problem, just can’t figure out how its done.

I need to check which payment option was selected by the customer.

Read More

Problem here, i dont know where to “hook” it so it will update itself whenever another option is selected.
Say: It gets updatet If the customer selects “Paypal” instead of the preselected option “Bank transfer”.

With the following code i can print out the selected payment option, bacs ( Bank-Transfer ), but it’s the standart one ( it’s preselected at page load ) and it wont change if i select paypal or another payment option.

add_filter( 'woocommerce_available_payment_gateways', 'filter_gateways', 1);
function filter_gateways( $gateways ){
  global $woocommerce;

 $chosen_gateway = $woocommerce->session->chosen_payment_method;
 if ( $chosen_gateway == 'bacs' ) {
  echo $chosen_gateway;
 }


  return $gateways;

}

However, i think i just dont understand the hooks or sth. This code wont get update if i click on “paypal” instead of “bacs”.
It shows “bacs” on Page loading and wont change.

I know the reason, it’s triggered at “woocommerce_available_payment_gateways” because i managed to disable payment methods for specific products in cart.

But i can’t find the hook for “after_selected_payment_method”

I’m sorry if my text isn’t clear enough, just to tell you what i want to achieve with the finished code:

First it should check the selected payment option ( Paypal, Bacs, .. )
After this it compares the number of string entries ( names ) in the database.
It than selects the (in this example) paypal address of the user whose name appears more often in the database. If equal, it makes it random. If there is a big difference, lets say 4 entries to 20, the chance to be picked is higher for the user with 20 entries .

If paypal was selected, it overwrites the paypal address in checkout, if Bank-Transfer was selected, it overwrites the Bank data and so on.

I hope you get what i mean.
I’d really appreciate if someone can help me. Thank you!

Related posts

Leave a Reply