The variable $parameter['pay'] can receive values ​​in the form of names: Cash Non-cash

It is necessary to replace these values ​​with numbers:

Cash = 1

Cashless = 2

 add_action('woocommerce_thankyou', 'send_order'); function send_order( $order_id ){ // Get an instance of the WC_Order object $order = wc_get_order( $order_id ); $order_data = $order->get_data() $parameter['pay'] = $order_data['payment_method_title']; } 

As a result, the parameter $parameter['pay'] will have the value 1 or 2.

Tried this option, but gives a syntax error.

 add_action('woocommerce_thankyou', 'send_order'); function send_order( $order_id ){ // Get an instance of the WC_Order object $order = wc_get_order( $order_id ); $order_data = $order->get_data() $parameter['pay'] = $payment_method_code; $payment_method_code = $order_data['payment_method_title']; $cash = str_replace("Наличные", "1", "$payment_method_code"); $terminal = str_replace("Безналичный", "2", "$payment_method_code"); print_r ($parameter['pay']) } 

    1 answer 1

     $arr = array( "Наличные" => 1, "Безналичный" => 2 ); $parameter['pay'] = $arr[$parameter['pay']]; 

    We first declared an array containing all the associations that we need, and then replaced the contents of our variable with a value from an array whose key corresponds to the initial value of our variable.

    only if your form changes and other lines are sent will it stop working. It’s better to turn the form over so that the numeric values ​​immediately come and there are no dependencies on the text.

    • Thank you, but you forgot the line $order_data['payment_method_title']; - it receives the initial data. - Alexandra Kuznetsova
    • I have described the principle itself as a text replaced with an associative value. by the way, in the above code (which gives an error), it would not be bad for you to swap the first 2 lines with $payment_method_code , i.e. on the line $parameter['pay'] = $payment_method_code; the $payment_method_code variable $payment_method_code not yet exist, respectively, and $parameter['pay'] will be empty - Maximmka
    • In this case, it would be more accurate as it seems to me: $ arr = array ("Cash" => 1, "Non-cash" => 2); $ parametr ['pay'] = $ arr [$ order_data ['payment_method_title']]; - Alexandra Kuznetsova