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()
Related
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);
this is very strange, i am calling $order = new WC_Order(52); and I get a correct order object but the Items array is empty.
Any ideas whats going wrong ?
I am using WooCommerce 3.4.3.
I also thought what you are doing should work but in fact it is wrong. I read the WooCommerce source code and this is how WooCommerce does it.
if ( ! $order = wc_get_order( $order_id ) ) {
return;
}
$order_items = $order->get_items( apply_filters( 'woocommerce_purchase_order_item_types', 'line_item' ) );
The $order->get_items() call will check if the line item has been read from the data store and read it if it has not yet been read. So, new WC_Order(52) actually only partially initializes the order object. The order items are initialized later when they are accessed through the order. This may be more efficient as items that are not used will not be initialized. Please note that the order is created by using the wc_get_order() function. You should do this instead of new WC_Order() as this uses the factory to create the order.
The solution provided here is correct. However if you are trying to get the order items on woocommerce_new_order it will not solve your problem since line items are assigned to the order after the woocommerce_new_order hook is triggered.
I only managed to sort my issues after I changed the hook to woocommerce_checkout_order_processed as per below:
add_action( 'woocommerce_checkout_order_processed', 'get_order_items_on_checkout', 50, 3 );
function get_order_items_on_checkout($order_id, $posted_data, $order){
$items = $order->get_items();
}
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 );
I want to make some changes if the chosen payment method is COD. On existing WC_Order i have used
($order->payment_method_title == 'Cash On Delivery' ? ... : ... );
to retrieve the title.
But i would like to check against the id (cod) because the title string gets translated to different languages which doesn't make it a good solution.
Is there a way to retrieve the id on a WC_Order in woocommerce?
The post meta key for the payment method ID is simply _payment_method
So if $order->payment_method doesn't have the magic methods in place to get that automatically, you could retrieve the post meta using traditional WordPress
get_post_meta( $order->id, '_payment_method', true );
Update for WooCommerce 3.0
$order->get_payment_method();
If you want the title of the payment method you can do:
$order = new WC_Order( $order_id );
$payment_title = $order->get_payment_method_title();
This returns the string set in the Woocommerce > Payment methods, ex: Paypal.
Here are some very helpful references and documentation that will help you do anything you want with WooCommerce.
WooCommerce Order Class
WooCommerce Reference Docs
Cheers.
If you need the payment gateway object itself you can use the wc_get_payment_gateway_by_order function.
$payment_gateway = wc_get_payment_gateway_by_order( $order );
I have found method
$order->get_shipping_method()
to access the name, but i want to retrieve the id instead of name?
thought i'd share how I solved this if someone runs into the same problem as me. I have WC_Order in the $order variable.
$order->get_items( 'shipping' );
This gives me an array with name, type, method_id, cost and taxes.
$shipping_method = #array_shift($order->get_shipping_methods());
$shipping_method_id = $shipping_method['method_id'];