Woocommerce get order key - php

I have orders in the format [domain]/checkout/order-received/[order_number]/key=[wc-order-key] - how do I get [wc-order-key]?
So far I've done:
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id)
{
$order = new WC_Order( $order_id );
$myuser_id = (int)$order->user_id;
$user_info = get_userdata($myuser_id);
$items = $order->get_items();
foreach ($items as $item)
{
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$product_description = get_post_meta($item['product_id'])->post_content
}
return $order_id;
}

If i understand correctly, you need get order_key by order_id, is it correct?
If so, you can just use WC_Order property:
$test_order = new WC_Order($order_id);
$test_order_key = $test_order->order_key;
Edited
As mentioned indextwo, since Woo 3.0 there new syntax:
$test_order = wc_get_product($order_id);
$test_order_key = $test_order->get_order_key();

2018 updated answer
As WooCommerce 3 changed how property calls were made, the appropriate way to get the same information is:
$order = wc_get_order($order_id);
// Added a check to make sure it's a real order
if ($order && !is_wp_error($order)) {
$order_key = $order->get_order_key();
}
Note that you can easily do the same in reverse: get an order ID from an order key:
$order_id = wc_get_order_id_by_order_key($order_key);

Related

Get product IDs while using the "woocommerce_payment_complete" hook

My hooks are in theme folder/functions.php
I want tie hook with product_id from order.
tryin this method but they return nothing.
$product->get_id()
or
$product = wc_get_product( $product_id );
Full code
add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete($order_id)
{
$order = wc_get_order($order_id);
$billingEmail = $order->billing_email;
$billingName = $order->billing_first_name;
$product_id = $order->product_id;
if ($product_id == 980) {
......
}
If you use Woocommerce 3.0+ version, then it should be this.
I found the answer with this link: https://wordpress.stackexchange.com/questions/97176/get-product-id-from-order-id-in-woocommerce
In an order can be multiple products, so you have to loop through them. In your code it would look like this:
add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete($order_id)
{
$order = wc_get_order($order_id);
$billingEmail = $order->billing_email;
$billingName = $order->billing_first_name;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
$product_variation_id = $item->get_variation_id();
if ($product_id == 980) {
// ....
}
}
}

how to get woocommerce shipping class price/cost using shipping class id

I am trying to get shipping class cost assigned to products.
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ($items as $item) {
$product = new WC_Product($item['product_id']);
$shipping_class_id = $product->get_shipping_class_id();
}
i don't know how to get the cost of the shipping class based on its id can anyone help me?
you can try this
foreach ($order->get_items('shipping') as $item_id => $shipping)
{
$method_id = $shipping->get_method_id();
$name = $shipping->get_name();
$total = $shipping->get_total();
}

How to get all order details in wordpress/woocommerce? [duplicate]

This question already has answers here:
How to get WooCommerce order details
(6 answers)
Closed 4 years ago.
I am using woo-commerce and trying to get all the order details from database.I am new to it and cant make it happened.I have already created order_details.php template where I can get all orders data.
Can anyone help me out?
Try This Code.
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);
$orders = $my_query->posts;
echo "</pre>";
print_r($orders);
echo "<pre>";
Customize As par Ur Requerment.
$my_course_query = new WP_Query(array('post_type'=>'shop_order','post_status'=>'wc-completed'));
Change status to woocoommerce status
Since Woocommerce mega major Update 3.0+ things have changed quite a lot:
WC_Order properties can't be access directly as before and will throw some errors.
New WC_Order and WC_Abstract_Order getters and setters methods are now needed.
There is some New classes for Order items:
WC_Order_Item class.
WC_Order_Item_Product class.
So also the Order items properties will not be accessible as before in a foreach loop and you will have to use this specific getter and setter methods instead
// Get an instance of the WC_Order object (same as before)
$order = wc_get_order( $order_id );
// Get the order ID
$order_id = $order->get_id();
// Get the custumer ID
$order_id = $order->get_user_id();
Get and access to the order data properties (in an array of values):
$order = wc_get_order( $order_id );
$order_data = $order->get_data(); // The Order data
$order_id = $order_data['id'];
$order_parent_id = $order_data['parent_id'];
$order_status = $order_data['status'];
$order_currency = $order_data['currency'];
$order_version = $order_data['version'];
$order_payment_method = $order_data['payment_method'];
$order_payment_method_title = $order_data['payment_method_title'];
$order_payment_method = $order_data['payment_method'];
$order_payment_method = $order_data['payment_method'];
## Creation and modified WC_DateTime Object date string ##
// Using a formated date ( with php date() function as method)
$order_date_created = $order_data['date_created']->date('Y-m-d H:i:s');
$order_date_modified = $order_data['date_modified']->date('Y-m-d H:i:s');
// Using a timestamp ( with php getTimestamp() function as method)
$order_timestamp_created = $order_data['date_created']->getTimestamp();
$order_timestamp_modified = $order_data['date_modified']->getTimestamp();
$order_discount_total = $order_data['discount_total'];
$order_discount_tax = $order_data['discount_tax'];
$order_shipping_total = $order_data['shipping_total'];
$order_shipping_tax = $order_data['shipping_tax'];
$order_total = $order_data['cart_tax'];
$order_total_tax = $order_data['total_tax'];
$order_customer_id = $order_data['customer_id']; // ... and so on
## BILLING INFORMATION:
$order_billing_first_name = $order_data['billing']['first_name'];
$order_billing_last_name = $order_data['billing']['last_name'];
$order_billing_company = $order_data['billing']['company'];
$order_billing_address_1 = $order_data['billing']['address_1'];
$order_billing_address_2 = $order_data['billing']['address_2'];
$order_billing_city = $order_data['billing']['city'];
$order_billing_state = $order_data['billing']['state'];
$order_billing_postcode = $order_data['billing']['postcode'];
$order_billing_country = $order_data['billing']['country'];
$order_billing_email = $order_data['billing']['email'];
$order_billing_phone = $order_data['billing']['phone'];
## SHIPPING INFORMATION:
$order_shipping_first_name = $order_data['shipping']['first_name'];
$order_shipping_last_name = $order_data['shipping']['last_name'];
$order_shipping_company = $order_data['shipping']['company'];
$order_shipping_address_1 = $order_data['shipping']['address_1'];
$order_shipping_address_2 = $order_data['shipping']['address_2'];
$order_shipping_city = $order_data['shipping']['city'];
$order_shipping_state = $order_data['shipping']['state'];
$order_shipping_postcode = $order_data['shipping']['postcode'];
$order_shipping_country = $order_data['shipping']['country'];
Get the order items and access the data with WC_Order_Item_Product and WC_Order_Item methods:
$order = wc_get_order($order_id);
// Iterating through each WC_Order_Item_Product objects
foreach ($order->get_items() as $item_key => $item_values):
## Using WC_Order_Item methods ##
// Item ID is directly accessible from the $item_key in the foreach loop or
$item_id = $item_values->get_id();
## Using WC_Order_Item_Product methods ##
$item_name = $item_values->get_name(); // Name of the product
$item_type = $item_values->get_type(); // Type of the order item ("line_item")
$product_id = $item_values->get_product_id(); // the Product id
$wc_product = $item_values->get_product(); // the WC_Product object
## Access Order Items data properties (in an array of values) ##
$item_data = $item_values->get_data();
$product_name = $item_data['name'];
$product_id = $item_data['product_id'];
$variation_id = $item_data['variation_id'];
$quantity = $item_data['quantity'];
$tax_class = $item_data['tax_class'];
$line_subtotal = $item_data['subtotal'];
$line_subtotal_tax = $item_data['subtotal_tax'];
$line_total = $item_data['total'];
$line_total_tax = $item_data['total_tax'];
endforeach;
You can user below code
require(dirname(__FILE__) . '/wp-load.php');
global $wpdb;
$orders = get_posts( array(
'post_type' => 'shop_order',
'posts_per_page' => '-1'
) );
You need to include wp-load.php file
require(dirname(__FILE__) . '/wp-load.php');
and add global variable as global $wpdb; before this code.

WC_Order get_items() and their Quantity

I am trying to build an application which texts me my woo commerce order, order items and Quantity,
I am 90% there,
function custom_woocommerce_complete_order_sms( $order_id ) {
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$product_list = '';
$order_item = $order->get_items();
foreach( $order_item as $product ) {
$prodct_name[] = $product['name'];
}
$product_list = implode( ',\n', $prodct_name );
require "twilio-php-master/Services/Twilio.php";
$AccountSid = "xxxxxxxxx";
$AuthToken = "xxxxxxxxx";
$client = new Services_Twilio($AccountSid, $AuthToken);
$people = array(
"xxxxxxxxxx" => "Me",
);
foreach ($people as $number => $name) {
$sms = $client->account->messages->sendMessage(
"+44xxxxxxxxxx",
// the number we are sending to - Any phone number
$number,
// the sms body
"Hey $name, there is a new Order, the order is, $product_list"
);
}
}
My problem is I do not know how to get the item Quantity , for example my text looks like list, item 1, item 2, item 3, I want it to say item 1 x1, item 2 x2, item3 x3
I did try and dig into the email php file in abstract woo commerce folder to see how they do as they send Quantities in emails but got a little lost
also in the class WC_Abstract_Order the only other thing I could find is get_item_total which returns to the total of all items
From research you can also grab the qty from the order item
$product['qty'];
Therefore , it was simple to loop over and add the quantity to the item name (below)
$product_details = array();
$order_items = $order->get_items();
foreach( $order_items as $product ) {
$product_details[] = $product['name']."x".$product['qty'];
}
$product_list = implode( ',', $product_details );

WooCommerce - Get the product description by product_id

How can I get the product description or the product object using product ID.
$order = new WC_Order($order_id);
foreach ($order->get_items() as $item) {
$product_description = get_the_product_description($item['product_id']); // is there any method can I use to fetch the product description?
}
The code above extends class WC_Payment_Gateway
Any help would be greatly appreciated.
$order = new WC_Order($order_id);
foreach ($order->get_items() as $item)
{
$product_description = get_post($item['product_id'])->post_content; // I used wordpress built-in functions to get the product object
}
If you are using WooCommerce 3.0 or above, then you can get description with the below code.
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item) {
$product_id = $item['product_id'];
$product_details = $product->get_data();
$product_full_description = $product_details['description'];
$product_short_description = $product_details['short_description'];
}
Alternate way (recommended)
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item) {
$product_id = $item['product_id'];
$product_instance = wc_get_product($product_id);
$product_full_description = $product_instance->get_description();
$product_short_description = $product_instance->get_short_description();
}
Hope this helps!

Categories