WooCommerce - change order status with php code - php

I am trying to change order status in WooCommerce, but I encountered no luck so far. $order instance is created successfully (I know it because echo $order->status; works fine, $order_id is also correct. $order->status = 'pending'; simply doesn't change anything, I do not know why.
$order = new WC_Order($order_id);
$order->status = 'pending';
Could anyone help me with this?

Try this code:
$order = new WC_Order($order_id);
$order->update_status('pending', 'order_note'); // order note is optional, if you want to add a note to order

Working with woocommerce v4.4, other answers were not working for me. I had to do it this way,
$order = wc_get_order($order_id);
$order->set_status('pending');
$order->save();
Note: Woocommerce internally adds wc prefix, you will see it if you view in the database. We do not need to explicitly add it.

Since Woocommerce version 3.0+ to update status you need to do this
$order = wc_get_order( $order_id );
if($order){
$order->update_status( 'pending', '', true );
}

Related

How to run a shortcode for WooCommerce view-order template

I'm trying to insert a shortcode in the woocommerce view-order.php template but it doesn't work.
This is the reference template: https://woocommerce.github.io/code-reference/files/woocommerce-templates-myaccount-view-order.html
In the functions.php file I wrote the following code:
add_shortcode( 'order_view_id' , 'order_view_01' );
function order_view_01(){
$customer_id = get_current_user_id();
$order = new WC_Order( $order_id ); //I think this is the problem I don't know if that's right
return $order->get_id();
}
The shortcode shows the number 0, so I'm not getting the order id which in my case is 40001.
To structure the code I followed these references:
How to create a shortcode for Woocommerce view-order template?
Create Woocommerce shortcodes with order details
Maybe I should change the line that affects the $order part, but I'm not sure.
I don't understand where I'm wrong, does anyone kindly have a suggestion?
Have found a solution.
After some research I came across this post: How can I get the order ID from the order Key in WooCommerce?
After that I tried to insert this $order_id = absint( get_query_var('view-order') ); Everything worked fine. Here is the solution for anyone in the same situation.
add_shortcode( 'order_view_id' , 'order_view_01' );
function order_view_01(){
// Get Order ID
$order_id = absint( get_query_var('view-order') );
// Then you can get the order object
$order = new WC_Order( $order_id );
// What you want to see, in my case the order ID
return $order->get_id();
}
Here you can find everything you are interested in showing via shortcode: https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/
Remember, I wrote the code in functions.php. This allows me to insert the [order_view_id] shortcode inside the woocommerce view-order.php template.

Woocommerce: change order id after payment is completed

I have made a simple webshop with woocommerce, with three payment methods. iDeal and by direct bank transfer and on account. The order ID is created based on the payment method. for example, if payment is made with iDEAL, the order id becomes ID190100; if payment is made on account, the order id becomes RK190100. I get this working with the plugin
"Sequential Order Numbers for WooCommerce" from BeRocket but these are already created before the payment is complete. The order ID must only be finalized once the payment has been made. Now orders that have not yet paid, and may not be paying, will receive a fixed order ID. So is it possible to create a temporary order id and when the order is completed change the order id based on payment method?
Woocommerce by default will use the post ID of the order for the order ID. This is evident when viewing the WC_Order::get_order_number() method. If you want to use a custom order number to display, you'll need to add a filter on woocommerce_order_number to load in a different value.
An example script would be:
add_action( 'woocommerce_order_status_completed', 'wc_change_order_id' );
function wc_change_order_id( $order_id ) {
$order = wc_get_order( $order_id );
$method = $order->get_payment_method(); // need check this
if ( $method === 'account' ) {
$number = 'ID' . $order->get_id();
$order->update_meta_data('_new_order_number', $number );
}
}
add_filter('woocommerce_order_number', function($default_order_number, \WC_Order $order) {
//Load in our meta value. Return it, if it's not empty.
$order_number = $order->get_meta('_new_order_number');
if(!empty($order_number)) {
return $order_number;
}
// use whatever the previous value was, if a plugin modified it already.
return $default_order_number;
},10,2);
Try this. It's very quick example. Hope help.
add_action( 'woocommerce_order_status_completed', 'wc_change_order_id' );
function wc_change_order_id( $order_id ) {
$order = wc_get_order( $order_id );
$method = $order->get_payment_method(); // need check this
if ( $method === 'account' ) {
$number = 'ID' . $order->get_id();
$order->set_id( $number );
$order->save();
}
}

setting custom order status in woocommerce

I have followed this tutorial and to add a custom order status "Awaiting Shipment":
My problem is I'm trying to update the status via a php function, but it stays sets on pending payment! So it is executing and changing the correct order but not with this new status.
My code:
$order = new WC_Order($order_id);
$order->update_status('Awaiting shipment', 'order_note');
I can set 'Awaiting Shipment' in the WordPress dashboard ok...
What am I doing wrong?
You need to set it using the slug awaiting-shipment instead, so your code will be:
$order = new WC_Order( $order_id );
$order->update_status('awaiting-shipment', 'order_note');
This time it will work…
Also 'order_note' is optional and should be replaced with a real explicit text as an order note should be.
To finish you also can use $order = wc_get_order( $order_id );
Reference: WC_Order update_status() method
Related thread: WooCommerce: Auto complete paid orders
Try this below
add_action( 'woocommerce_thankyou', 'my_custom_status_update' );
function my_custom_status_update( $order_id ) {
$order = new WC_Order( $order_id );
$order->update_status( 'awaiting-shipment' );
}

WooCommerce codex : how to set the user/customer on an order

I am writing a WooCommerce Plugin that takes care of payment and delivery.
Right now I am at the point of creating an order based on the current shopping cart.
That all works fine getting the items and costs correct, the only problem is that the order shows as being made by "Guest", instead of by the currently logged on user (even though the correct email address for that user is on the order).
Here is my code :
$cart = WC()->cart;
$checkout = WC()->checkout();
$order_id = $checkout->create_order();
$order = wc_get_order( $order_id );
$order->user_id = apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() );
$order->calculate_totals();
$order->payment_complete();
$cart->empty_cart();
Here is what I see in the backend after running this :
Why is the order placed as "Guest" instead of the user that placed the order?
And how do I get to have the correct user attached?
You will need to add _customer_user to post_meta table with order ID and meta value should be the user ID which you want to link to this order.
I hope that this will help you:
update_post_meta($order_id, '_customer_user', get_current_user_id());
OR
use user/customer id(create a customer/user before place order) in wc_create_order();
eg: $order = wc_create_order(array('customer_id' => $user));
this code fine for me!

Woocommerce custom stock update after successful order

I have a custom database table linked to my Woocommerce tables with some information like SKU and Stock. I want to add custom function to update this table after successful order (when Woocommerce updates product stock). I've tried to do something with this:
add_action( 'woocommerce_reduce_order_stock', 'wpet_testnote' );
function wpet_testnote() {
// Lets grab the order
$order = wc_get_order( $order_id );
$order->add_order_note( 'Stock Updated.' );
I've tried to testing my action with basic order note adding, but it's not working. Any ideas?
you forgot to submit the $order_id. this one works on my end:
add_action( 'woocommerce_reduce_order_stock', 'wpet_testnote' );
function wpet_testnote( $order_id ) {
$order = wc_get_order( $order_id );
$order->add_order_note( 'Stock Updated.' );
}

Categories