Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce: Update order's payment method WordPress

  • SOLVED

Hi,

Given an order ID, I need to update its <strong>payment method</strong> to COD (Cash on delivery) from a stand-alone PHP script (placed outside of the wordpress' root directory)

I've been using the Woocommerce REST API but it's limited for what I'm asking.

I want the script to access to the wordpress DB, so I would need the SQL query to update an order's payment method to "cod".

I've seen some answers that use the wc_query class to access to the DB but, because I'm a newbie, I don't know how to use it properly. So if your solution uses the WC_Query class tell me how I include it in my stand-alone PHP script

Thanks!

Answers (2)

2014-03-05

Dbranes answers:

You can try this, if your <em>script.php</em> is in the WP root directory:

<?php

/**
* Update payments methods for a given order
*
* file: script.php
*/

define( 'WP_USE_THEMES', FALSE );
require( './wp-blog-header.php' );

// EDIT:
$order_id = 529;
$payment_method = 'paypal';
$payment_method_title = 'PayPal';

$order_id = 540;
$payment_method = 'cod';
$payment_method_title = 'Cash on delivery';


// before update:
echo 'payment_method: ' . get_post_meta( $order_id , '_payment_method', TRUE );
echo '<br/>';
echo ' payment_method_title: ' . get_post_meta( $order_id, '_payment_method_title', TRUE );

// update payment method
update_post_meta( $order_id, '_payment_method', $payment_method );
update_post_meta( $order_id, '_payment_method_title', $payment_method_title );

//after update:
echo '<hr/>';
echo 'payment_method: ' . get_post_meta( $order_id , '_payment_method', TRUE );
echo '<br/>';
echo ' payment_method_title: ' . get_post_meta( $order_id, '_payment_method_title', TRUE );


Paul August comments:

Hi Dbranes! That totally worked !
I want to change the order status to 'complete'.. How can I achieve that?


Dbranes comments:

You can change the order status by modifying the <em>shop_order_status</em> custom taxonomy for the given order.

So you can use for example:



//$order_status = 'cancelled';
$order_status = 'completed';

wp_set_object_terms( $order_id, $order_status, 'shop_order_status', FALSE );


to change it.


Paul August comments:

I really appreciate your answek Dbranes !

Big thanks !


Dbranes comments:

Glad to hear it worked for you.

2014-03-05

Bob answers:

your custom script is in root directory.
if it is with in root of wordpress installation.

then call wp-load.php at the top.

now you can user wordpress function and hopefully wc_query also.


Bob comments:

include('wp-load.php');


Bob comments:

if only wp-load.php do not work then you might have to include wp-blog-header.php

require_once ('/path/to/wordpress/wp-load.php');
require_once ('/path/to/wordpress/wp-blog-header.php');