Replace a payment method title for an external shipping service in Woocommerce 3

I am using Woocommerce and an external shipping service. I have a $parameter['pay'] value that can have a string value that can be "Cash" or "Terminal". I would like to replace those strings with a number like:

Here is my actual code where I would like to do that:

add_action('woocommerce_thankyou', 'send_order_to_delivery');
function send_order_to_delivery( $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'];
}

So the $parameter['pay'] should be 1 or 2.

I have tried this:

add_action('woocommerce_thankyou', 'send_order_to_delivery');
function send_order_to_delivery( $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("Cash", "1", "$payment_method_code");
    $terminal = str_replace("Terminal", "2", "$payment_method_code");
}

But it throws an error.

How can I make that works? Any help is appreciated.

Upvotes: 1

Views: 935

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254009

First the error comes from those 2 lines:

$cash = str_replace("Cash", "1", "$payment_method_code");
$terminal = str_replace("Terminal", "2", "$payment_method_code");

They should need to remove the " character from the variables to have this instead:

$cash = str_replace("Cash", "1", $payment_method_code);
$terminal = str_replace("Terminal", "2", $payment_method_code);

It will solves your error problem.


Now you could try the following, using WC_Order get_payment_method_title() method:

add_action('woocommerce_thankyou', 'send_order_to_delivery');
function send_order_to_delivery( $order_id ){

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    $pay = array("Cash" => 1, "Terminal" => 2); 

    $param['pay'] = $pay[$order->get_payment_method_title()];
}

It could work.

Upvotes: 1

Related Questions