In Woocommerce, I just want to get "local pickup" shipping details to display on a custom email. I tried below functions but they don't show anything for "local pickup".
Which function I can use to get "local pickup" info?
I tried without success the following WC_Order methods:
$order->get_shipping_address_1()
$order->get_formatted_shipping_address()
Edit:
Sorry I did not mention that, but I am using Local Pickup Plus plugin
Edit 2:
This is how I got local pickup info for Local Pickups Plus Plugin docs.woocommerce.com/document/local-pickup-plus which puts meta data to main order variable.
$order = wc_get_order( $order_id );
foreach ($order->get_data() as $key => $value):
if ($key==='shipping_lines'):
foreach ($value as $k=>$v):
$a = $v->get_meta_data();
foreach ($a as $x=>$y):
$t = $y->get_data();
$mykey = $t['key'] ;
$pickup["$mykey"] = $t['value'];
endforeach;
endforeach;
endif;
endforeach;
Then you can use the variables below:
$pickup['_pickup_location_name']
$pickup['_pickup_location_address']['address_1']
$pickup['_pickup_location_phone']['address_2']
$pickup['_pickup_location_address']['postcode']
$pickup['_pickup_location_address']['city']
$pickup['_pickup_location_address']['state'] $pickup['_pickup_location_address']['country']
$pickup['_pickup_location_phone']
$pickup['_pickup_date']
$pickup['_pickup_minimum_hours']
For Order items shipping details refer to: "Get orders shipping method details in WooCommerce 3"
To target order shipping lines details from the WC_Order object you can use the following code:
// Loop though order items shipping
foreach( $order->get_shipping_methods() as $item_id => $item ){
$shipping_item_name = $item->get_name();
$shipping_item_type = $item->get_type();
$shipping_method_title = $item->get_method_title();
$shipping_method_id = $item->get_method_id();
$shipping_method_instance_id = $item->get_instance_id();
$shipping_method_total = $item->get_total();
$shipping_method_total_tax = $item->get_total_tax();
$shipping_method_taxes = $item->get_taxes();
// Get custom meta-data
$formatted_meta_data = $item->get_formatted_meta_data( ' ', true );
// Displaying the row custom meta data Objects (just for testing)
echo '<pre>'; print_r($formatted_meta_data); echo '</pre>';
}
Regarding the custom shipping metadata:
You can access it using the WC_Data method get_meta() from the custom meta "key" located in any custom meta data Objects, like:
$value = $item->get_meta('the_custom_key'); // 'the_custom_key' need to be replaced by the meta "key".
Note: In most Woocommerce email templates and email notification related hooks, you can use the WC_Order object as it's globally included. If not you can get it from the Order ID like:
$order = wc_get_order( $order_id );
Orders related threads:
Get orders shipping method details in WooCommerce 3
Get Order items and WC_Order_Item_Product in Woocommerce 3
How to get WooCommerce order details
Addition - For Local Pickup Plus plugin
It seems that you are using Local Pickup Plus plugin which adds specific custom meta data in the shipping lines.
// Loop though order items shipping
foreach( $order->get_shipping_methods() as $item_id => $item ){
$location_id = $item->get_meta('_pickup_location_id');
$location_name = $item->get_meta('_pickup_location_name');
$location_address = $item->get_meta('_pickup_location_address'); // Array
$location_address_1 = $location_address['address_1'];
$location_address_2 = $location_address['address_2'];
$location_postcode = $location_address['postcode'];
$location_city = $location_address['city'];
$location_state = $location_address['state'];
$location_country = $location_address['country'];
$location_phone = $item->get_meta('_pickup_location_phone');
$pickup_date = $item->get_meta('_pickup_date');
$pickup_min_hours = $item->get_meta('_pickup_minimum_hours');
}
Related
How can I retrieve the shipping methods that are visible to the end user? ( not all shipping methods defined in Woocommerce ). I am using the "Shipping Zones by Drawing for WooCommerce" plugin. I need multiple radiuses around the store. The issue is that I have more than one and this plugin will only hide the radius where the user is located outside of it and will show the rest ( I need to show only one of them, the cheapest ).
I have tried to print the rates from woocommerce_package_rates and WC()->session but these will show all shipping methods defined including the one that it is not shown the user.
To get the costumer available shipping methods when shipping location is defined and when cart is not empty, you can use:
// Get shipping packages
$shipping_packages = WC()->cart->get_shipping_packages();
foreach( array_keys( $shipping_packages ) as $key ) {
if( $shipping_for_package = WC()->session->get('shipping_for_package_'.$key) ) {
if( isset($shipping_for_package['rates']) ) {
// Loop through customer available shipping methods
foreach ( $shipping_for_package['rates'] as $rate_key => $rate ) {
$rate_id = $rate->id; // the shipping method rate ID (or $rate_key)
$method_id = $rate->method_id; // the shipping method label
$instance_id = $rate->instance_id; // The instance ID
$cost = $rate->label; // The cost
$label = $rate->label; // The label name
$taxes = $rate->taxes; // The taxes (array)
print_pr($label);
}
}
}
}
Now to get the chosen shipping method you will use:
WC()->session->get('chosen_shipping_methods'); // (Array)
In Woocommerce I am trying to display the results of order item object and to access it:
$product_meta = $item->get_meta_data();
print_r ($product_meta);
This is what I am trying to pull:
EDIT: This is the output that I get using $item->get_formatted_meta_data( '', true ):
To get all order item meta data, you will use WC_Order_Item get_formatted_meta_data() method with specific arguments, this way:
// Accessible non protected Order item meta data
$item_meta_data = $item->get_formatted_meta_data( '', true );
// Formatted raw Output
echo '<pre>'; print_r($item_meta_data); echo '</pre>';
To access some order item properties, you can use any WC_Order_Item_Product method like:
$item->get_product(); // Get the WC_Product object
$item->get_product_id(); // Get the Product ID
$item->get_variation_id(); // Get the Variation ID
$item->get_name(); // Get the Product name
$item->get_quantity(); // Get the item quantity
// and so on …
Then if you need to access a specific "custom" order item data value, you will use WC_Data get_meta() method:
$custom_value = $item->get_meta("_custom_key");
See: Get Order items and WC_Order_Item_Product in Woocommerce 3
Update (displaying your required custom order item meta data)
The data you need can be accessed and displayed this way:
if( $lessons = $item->get_meta('lessons') ) {
echo '<p>Lessons: '.$lessons.'</p>';
}
if( $tour_guide = $item->get_meta('tour guide') ) {
echo '<p>Tour Guide: '.$tour_guide.'</p>';
}
I hope that this works now.
All I did was put this wc_display_item_meta( $item );
and that is it , it pulls the info automatically !!!!!!!
admin can change those in the edit order screen to anything and they will appear
(thanks to #LoicTheAztec for pointing me to the right direction
I need to change the item price in a woocommerce order but everything I found is to changing the price in the cart but this is not what I need because I need to change after the checkout process.
Does somebody can give me a clue on how to do that?
You need to use the new CRUD setters methods introduced with Woocommerce 3:
For order object you will use WC_Order methods,
For order "line item" you will use WC_Order_Item_Product methods,
For both of them you could be also use some WC_Data methods like save()…
Here is a working basic example with a static price and a static order ID:
$order_id = 809; // Static order Id (can be removed to get a dynamic order ID from $order_id variable)
$order = wc_get_order( $order_id ); // The WC_Order object instance
// Loop through Order items ("line_item" type)
foreach( $order->get_items() as $item_id => $item ){
$new_product_price = 50; // A static replacement product price
$product_quantity = (int) $item->get_quantity(); // product Quantity
// The new line item price
$new_line_item_price = $new_product_price * $product_quantity;
// Set the new price
$item->set_subtotal( $new_line_item_price );
$item->set_total( $new_line_item_price );
// Make new taxes calculations
$item->calculate_taxes();
$item->save(); // Save line item data
}
// Make the calculations for the order and SAVE
$order->calculate_totals();
Then you will have to replace the static price by your submitted new price in your custom page, which is not so simple, as you will need to target the correct $item_id…
Thank you very much, I spent 4 hours looking for how to change the quantity of the product in the order and based on your code (I rewrote the necessary part) I finally got it! that's if someone needs to change the quantity product in the order `
$order = wc_get_order( $_POST['orderID'] );
foreach( $order->get_items() as $item_id => $item ){
$product = $item->get_product();
$product_price = (int) $product->get_price(); // A static replacement product price
$new_quantity = (int) $_POST['productQty'] // product Quantity
// The new line item price
$new_line_item_price = $product_price * $new_quantity;
// Set the new price
$item->set_quantity($_POST['orderQty']);
$item->set_subtotal( $new_line_item_price );
$item->set_total( $new_line_item_price );
// Make new taxes calculations
$item->calculate_taxes();
$item->save(); // Save line item data
}
// Make the calculations for the order and SAVE
$order->calculate_totals();`
#LoicTheAztec
Completing an automated woo-commerce Payment by manually inputting an identifier
The shopper goes online, creates an account , adds a payment method, and fills their cart .
We hold the amount plus 15% when they check out.
woocommerce sends order details to the delivery team that takes the gig .
They go to the store and shop
After checking out at the physical store, the new invoice total is uploaded to woo-commerce via the shopping app .
This manually entered amount will be the IDENTIFIER in the stripe that TRIGGERS the order completion
In Woocommerce I am trying to find out how to get the product id for a completed order inside the Customer completed order email template to store it as a PHP variable. This way I will be able to insert it into an external database.
I already tried $product->get_id(); but it does not work.
How can get the Product ID in WooCommerce 3+ from email templates?
You need to loop through Order items… Normally the WC_Order object is defined through the variable $order mostly defined everywhere in email templates.
So the code to get the product ID will be:
// Loop through order items
foreach( $order->get_items() as $item_id => $item ){
// Get the product ID
$product_id = $item->get_product_id();
// Get an instance of the WC_Product object
$product = $item->get_product();
// Get product title (name)
$product_name = $product->get_title(); // or $product->get_name();
// Get product price
$product_price = $product->get_price();
}
See this related thread: Get Order items and WC_Order_Item_Product in Woocommerce 3
With WooCommerce 3+ introducing new API to fetch the order and it's details, a lot of things have changed and many things break as well.
Consider the following code in my plugin:
$order = wc_get_order($order_id);
$id= 27;
var_dump($order->get_item($id));
which gives me bool(false). I have checked the database and the order and the item does exist.
Also
var_dump($order) does return the entire order object with all the items.
So basically, only the function get_item does not seem to work.
The only explanation is that the ID you are using is not an item_id with a type "line_item"…
I have tried and it works normally as expected using WC_Abstract_Order get_item() method when the item_id is of type "line_item".
To get and check the correct "line_item" Item IDs from a defined Order ID, try:
// define an exiting order ID first
$order_id = 422;
$order = wc_get_order($order_id);
foreach($order->get_items() as $item_id => $item_values){
$item_ids_array[] = $item_id;
}
var_dump( $item_ids_array ); // will output all item IDs (of type "line_item") for this order
## ==> Then now you can try (to check get_item() method):
foreach( $item_ids_array as $item_id ){
var_dump( $order->get_item( $item_id ) ); // Will output each WC_Order_Item_Product Object …
}
This should clarify things.
As reference: How to get WooCommerce order details