How to get the Order status changed date in Woocoomerce? - wordpress

I need to get the date in which the order status is changed.
For example 123 is order id , the order is created in 12-12-2015 , and order is confirmed in 13-12-2015 . So I need to know in which date order is confirmed from order id?

Never access order properties directly. Use this to get the order modified date:
$order = new WC_Order($order_id);
$modified_date = get_the_modified_date( $order );
echo $modified_date;

In woocommerce,
Try
$order = new WC_Order($order_id);
echo $order->modified_date;

Related

How to add an additional unique reference number to woocommerce orders

I would like to know how can I add a unique random number to my WooCommerce orders which are visible to the customer and at WooCommerce orders.
For example,
When there is a new order. It will have it's usual order ID and an additional reference ID.
This reference number has to be unique for each order. So that, can be used as a way to identify each order.
Thanks in advance :)
The basic idea is to generate a random number and add it as order meta. This would something like the following. This mayn't be the perfect one, but will lead you to it.
function create_reference_id( $order, $data ){
$random = write_function_for_random_id(); //Write the function to create random ID
$order->update_meta_data( 'custom_id', $random ); // replace custom_id with your meta key name.
}
add_action('woocommerce_checkout_create_order', 'create_reference_id', 10, 2);
This will add ID to WooCommerce Order object. Then you need to write code to show ID in email.
UPDATE
For the random number function you can use combination of order id and the value returned from wp_rand() function.
If that's the case then you need to write as following.
function create_order_reference_id( $order_id, $data ){
$random = $order_id.'_'.wp_rand(1, 1000);
$order = wc_get_order( $order_id );
$order->update_meta_data( 'custom_id', $random );
$order->save();
}
add_action('woocommerce_checkout_update_order_meta', 'create_order_reference_id', 10, 2);

How to get ACF data from WooCommerce customers for order export

On a Wordpress-Shop I use WooCommerce (WC) with Advenced-Custom-Fields (ACF)
and WP-All-Import (WPAI) + WP-All-Export (WPAE).
I added a ACF field CustomerNumber to the WC-Customer (which enhanced the WP-User).
On the WPAI-XML-Import I set the CustomerNumber with a value from a ERP.
So all customers have a unique CustomerNumber.
I now need to export the WC-Orders (to import them in the ERP again).
The Order-XML must include the CustomerNumber from the Customer belongs to the Order.
As I see, the other standard fields from the customer – like name and address – are copied automatically to the order (by WooCommerce itself).
My question is now: How I have to do this for the ACF’s?
Did I have to do this by code on my own? Adding the same AC-fields to the WC-Order and hook into the order checkout and copy the values from the customer to the order?
Or is there some kind of setup which do that and which I did not recognize?
Thx
I did not really found an answer.
My current solution is now as I described it in my question.
I added a new rule for these acf to also make them available on the orders.
Then I added a hook to new created orders, determine the acf from the user and copied the necessary values into the order acf.
function di_woocommerce_new_order( $order_id ) {
$order = new WC_Order( $order_id );
if ( !$order ) return;
$customer_id = $order->get_customer_id();
if ( !$customer_id ) return;
$customer = new WC_Customer($customer_id);
if ( !$customer ) return;
$customer_number = $customer->get_meta('customernumber');
if ( !$customer_number ) return;
// add customer number
$order->add_meta_data('customernumber', $customer_number);
// update order modified timestamp
$order->add_meta_data('order_last_updated', current_time( 'mysql' ));
$order->save_meta_data();
}
add_action( 'woocommerce_checkout_order_processed', 'di_woocommerce_new_order', 10, 1);

WooCommerce order-received endpoint and the order object

I am using the conditional is_wc_endpoint_url('order-received') in my header to check to see if the the 'page' is the Order Recieved end point, and if so I add some additional code to my Facebook Pixel code.
However I would like to access the order object as well so I can get the total of the order as well as check to see if it is actually complete ( get_status() ).
How can I access the order object from the order received end point?
Cheers
this worked for me
$order_key = $_GET['key'];
$order_id = wc_get_order_id_by_order_key($order_key);
$order = new WC_Order( $order_id );

How can I get the order date, in WooCommerce?

I can see inside class-wc-admin-cpt-shop_order.php there are some functions that are pulling together the order information for display in WooCommerce. However, I don't see anywhere where the date can be used ...
Because WooCommerce uses wp_posts to store the data, can I assume that the post_date field is the correct one to use?
Also, anyone know whether there is a function in WooCommerce to get this, or whether there is a way of getting the date to come out in class-wc-admin-cpt-shop_order.php.
// Get $order object from order ID
$order = wc_get_order( $order_id );
// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();
Source: https://businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/
Additionally
$order->get_date_created();
Is the "order date", which you can change within WooCommerce ("Edit Order")
You can use the WC_Order object, if you have the order ID:
$order = new WC_Order($order_id);
$order_date = $order->order_date;
Order properties should not be accessed directly. Best way is $order->get_date_completed()

How to get last inserted row ID from WordPress database?

My WordPress plugin has a table with a AUTO_INCREMENT primary key field called ID. When a new row is inserted into the table, I'd like to get the ID value of the insertion.
The feature is to using AJAX to post data to server to insert into DB. The new row ID is returned in the AJAX response to update client status. It is possible that multiple clients are posting data to server at the same time. So, I have to make sure that each AJAX request get the EXACT new row ID in response.
In PHP, there is a method called mysql_insert_id for this feature.But, it is valid for race condition only if the argument is link_identifier of last operation. My operation with database is on $wpdb. How to extract the link_identifier from $wpdb to make sure mysql_insert_id work? Is there any other way to get the last-inserted-row id from $wpdb?
Thanks.
Straight after the $wpdb->insert() that does the insert, do this:
$lastid = $wpdb->insert_id;
More information about how to do things the WordPress way can be found in the WordPress codex. The details above were found here on the wpdb class page
This is how I did it, in my code
...
global $wpdb;
$query = "INSERT INTO... VALUES(...)" ;
$wpdb->query(
$wpdb->prepare($query)
);
return $wpdb->insert_id;
...
More Class Variables
I needed to get the last id way after inserting it, so
$lastid = $wpdb->insert_id;
Was not an option.
Did the follow:
global $wpdb;
$id = $wpdb->get_var( 'SELECT id FROM ' . $wpdb->prefix . 'table' . ' ORDER BY id DESC LIMIT 1');
Get the last inserted row id in WP like this.
global $wpdb;
$order = ['product_name'=>'Computer', 'os_system'=>'Linux'];
$wpdb->insert('wp_product_orders', $order );
$last_inserted_id = $wpdb->insert_id;
Something like this should do it too :
$last = $wpdb->get_row("SHOW TABLE STATUS LIKE 'table_name'");
$lastid = $last->Auto_increment;
just like this :
global $wpdb;
$table_name='lorem_ipsum';
$results = $wpdb->get_results("SELECT * FROM $table_name ORDER BY ID DESC LIMIT 1");
print_r($results[0]->id);
simply your selecting all the rows then order them DESC by id , and displaying only the first
Putting the call to mysql_insert_id() inside a transaction, should do it:
mysql_query('BEGIN');
// Whatever code that does the insert here.
$id = mysql_insert_id();
mysql_query('COMMIT');
// Stuff with $id.

Resources