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 );
Related
I wish i could just use Uncle Google but he serve me only what i DON'T want :D
I have a free Product on my website. But I want to check every order manualy, because it's only for company accounts and not for "customers".
But every order will set the Status to approved - or what ever in english language- and the order is complete.
The user has automaticaly acces to "restricted area" and that's what I don't want. I want to check every order manualy and pick every spam account.
I can imagine, this is going to work with only one simple function but I can't get it. It's only one free Product, other Products are for moneeeeey.
It would be great if somebody has the same issue and can help me :)
Thank you
Place the following function in your active theme functions.php. Check all default statuses here and change on-hold to what you want - https://woocommerce.wp-a2z.org/oik_api/wc_get_order_statuses/
function change_free_order_status( $order_id ) {
if ( ! $order_id ) {return;}
$order = wc_get_order( $order_id );
if($order->get_total() <= 0):
$order->update_status( 'on-hold' ); // Change to what you need
endif;
}
add_action('woocommerce_thankyou','change_free_order_status');
I've created a hook for WooCommerce when an order is cancelled like so:
add_action( 'woocommerce_order_status_cancelled', 'prefix_order_cancelled_hook' );
function prefix_order_cancelled_hook($order_id){
write_log("Order ${order_id} has been cancelled")
}
Now when the status changed to cancelled, the hook is invoked as expected but the output execution is not what I'm expecting. I'm getting the following in the log:
Order 4 has been cancelled
Order 5 has been cancelled
Order 6 has been cancelled
I've seen that this corresponds to the number of orders I currently have on the store. Can someone help with why this is happening and how to only run the hook once for the changed order.
Seems like your code is executed for all orders. So there seems to be a problem with the $order_id. I think you can't access properties directly, but need to get an instance of the WC_Order object (since Woocommerce 3.0+ something about) like:
add_action( 'woocommerce_order_status_cancelled', 'prefix_order_cancelled_hook' );
function prefix_order_cancelled_hook($order_id){
// get an instance of an WC_Order object
$order = wc_get_order( $order_id );
// get the ID of the order
$order_id = $order->get_id();
write_log("Order ${order_id} has been cancelled")
}
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 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;
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()