How can i access current woocommerce order shipping method id? - wordpress

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'];

Related

Woocommerce : How to add a custom meta_key to checkout billing_phone?

I want to enter the value of digits_phone meta key to be entered as billing_phone for every woocommerce order.
I came up with something like this but it did not work :
//Automatically add the digits phone number of the user, to woocommerce orders for every order
add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data');
function dg_manipulate_checkout_posted_data ($data) {
$data['billing_phone'] =['digits_phone'];
return $data;
}
can anyone please help me to figure this out?
I have never used the plugin myself, take this as a guideline
THIS CODE IS NOT TESTED !
Maybe this question is a better fit for wordpress.stackexchange.com
From the last comment of this post I see that there should be 2 user metadata related to some digits_phone: 'digits_phone' and 'digits_phone_no'
Assuming that the one we want is digits_phone, this code should be a hint in the right direction:
add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data');
function dg_manipulate_checkout_posted_data ($data) {
// save current user data in variable $current_user
$current_user = wp_get_current_user();
//get digits phone from db and save in variable $digits_phone
$digits_phone = get_user_meta( $current_user->ID, 'digits_phone' , true );
// assign to POSTed array and return it
$data['billing_phone'] = $digits_phone;
return $data;
}
Also have a look at How can I convert woocommerce checkout fields in capital letters to get a better picture of manipulating POST data

WC_Order items empty

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();
}

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 check payment method on a WooCommerce order by 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 );

Resources