Get line item woocommerce order - php

I create html email template on woocomerce 3+ but i can't get line item by order id. I tried this but did not work for me.
<?php
$order = wc_get_order( $order_id );
$order_items = $order->get_items();
foreach ($order_items as $item_id => $item_data) {
$product_name = $item_data['name'];
$item_quantity = $order->wc_get_order_item_meta($item_id, '_qty', true);
$item_total = $order->wc_get_order_item_meta($item_id, '_line_total', true);
echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. $item_total;
}
?>
Any help very much appreciated.
Thanks.

Try with below code
add_action('init','orderLineItem');
function orderLineItem()
{
$orderId = 523; //put your dynamic order id or static id
$order = wc_get_order( $orderId );
foreach ( $order->get_items() as $item_key => $item_values ) {
$item_data = $item_values->get_data();
echo $product_name = $item_data['name'];
echo $quantity = $item_data['quantity'];
echo $line_total = $item_data['total'];
}
}

Related

Create order details shortcode for WooCommerce

Hey I am trying to build a shortcode for my order details on the order received page.
The code below will generate the last result and then on top of it, it will display the word Array. My guess is that something in the foreach loop i am creating is still an array, but I dont know what to do next.
thanks for any help.
function getOrderItemList(){
//set up array and Count
$item_data = '';
//get order ID
global $wp;
$order_id = absint( $wp->query_vars['order-received'] );
$order = wc_get_order( $order_id );
$order_line_items = $order->get_items();
//loop through each item in the order
foreach ($order_line_items as $item) {
$product = $item->get_product();
$product_id = $item->get_product_id();
$item_data = $item->get_data();
$product_name = $item->get_name();
$item_quantity = $item->get_quantity();
$item_total = $order->get_formatted_line_subtotal( $item );
$product_image = $product->get_image('order-received-item-image', $item);
$item_data .= '<tr class="order-item-row"><td class="order-item-image">' . $product_image . '</td><td class="order-item-name"><p>' . $product_name . ' x ' . $item_quantity . '</p></td><td class="order-item-total"><p>' . $item_total . '</p></td></tr>';
}
$item_list = $item_data;
$table .= <<<EOD
<table class="test">
$item_list
</table>
EOD;
return $table;
}
add_shortcode('order-line-item', 'getOrderItemList');
It looks like you jumbled a few things up, and had some unused variables in there. Try this.
function getOrderItemList() {
// set up array.
$item_list = '';
// get order ID.
global $wp;
$order_id = absint( $wp->query_vars['order-received'] );
$order = wc_get_order( $order_id );
$order_line_items = $order->get_items();
// loop through each item in the order.
foreach ( $order_line_items as $item ) {
$product = $item->get_product();
$product_name = $item->get_name();
$item_quantity = $item->get_quantity();
$item_total = $order->get_formatted_line_subtotal( $item );
$product_image = $product->get_image( 'order-received-item-image', $item );
$item_list .= '<tr class="order-item-row"><td class="order-item-image">' . $product_image . '</td><td class="order-item-name"><p>' . $product_name . ' x ' . $item_quantity . '</p></td><td class="order-item-total"><p>' . $item_total . '</p></td></tr>';
}
return "<table class=\"test\">$item_list</table>";
}
add_shortcode( 'order-line-item', 'getOrderItemList' );

Product meta data from order id WooCommerce

Attempting to pull product data from the last order such as [key] => pa_size
Using automatewoo_update_print_file to call file.php where the two function is located, it's called when a new note is added to an order:
function automatewoo_update_print_file( $workflow ) {
include '/home/***/public_html/wp-content/themes/***-child/woocommerce/checkout/file.php';
}
Update This worked well to pull the most recent order ID and get the product id, then the meta data from the ID and also the rest of the order data. But I still need to pull [key] => pa_size
function get_last_order_id(){
global $wpdb;
$statuses = array_keys(wc_get_order_statuses());
$statuses = implode( "','", $statuses );
// Getting last Order ID (max value)
$results = $wpdb->get_col( "
SELECT MAX(ID) FROM {$wpdb->prefix}posts
WHERE post_type LIKE 'shop_order'
AND post_status IN ('$statuses')
" );
return reset($results);
}
$latest_order_id = get_last_order_id(); // Last order ID
$order = wc_get_order( $latest_order_id ); // Get an instance of the WC_Order oject
$order_details = $order->get_data(); // Get the order data in an array
$order_status = $order_details['status'];
foreach ($order->get_items() as $item_key => $item ):
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$item_name = $item->get_name(); // Name of the product
$quantity = $item->get_quantity();
$product = $item->get_product(); // Get the WC_Product object
$product_price = $product->get_price();
endforeach;
$print_file = get_post_meta( $product_id, 'print_file_url', true );
// Raw output for testing
echo 'Product Price<pre> '; print_r( $product_price ); echo '</pre>';
echo 'Product Name<pre> '; print_r( $item_name ); echo '</pre>';
echo 'Product Quantity<pre> '; print_r( $quantity ); echo '</pre>';
echo 'Product ID<pre> '; print_r( $product_id ); echo '</pre>';
echo 'Variation ID<pre> '; print_r( $variation_id ); echo '</pre>';
echo 'Print File Url<pre> '; print_r( $print_file ); echo '</pre>';
echo 'Order Status<pre>'; print_r( $order_status ); echo '</pre>';
echo 'Latest Order ID<pre>'; print_r( $latest_order_id ); echo '</pre>';
echo 'Order Details<pre>'; print_r( $order_details ); echo '</pre>';
Use this function for get last order id
$last_order_id = wc_get_customer_last_order($user_id);
$order = wc_get_order( $order_id );
$order->get_items();
foreach ($order->get_items() as $item_key => $item ){
// Item ID is directly accessible from the $item_key in the foreach loop or
$item_id = $item->get_id();
## Using WC_Order_Item_Product methods ##
$product = $item->get_product(); // Get the WC_Product object
$product_id = $item->get_product_id(); // the Product id
$variation_id = $item->get_variation_id(); // the Variation id
$item_type = $item->get_type(); // Type of the order item ("line_item")
$item_name = $item->get_name(); // Name of the product
$quantity = $item->get_quantity();
$tax_class = $item->get_tax_class();
$line_subtotal = $item->get_subtotal(); // Line subtotal (non discounted)
$line_subtotal_tax = $item->get_subtotal_tax(); // Line subtotal tax (non discounted)
$line_total = $item->get_total(); // Line total (discounted)
$line_total_tax = $item->get_total_tax(); // Line total tax (discounted)
$product = $item->get_product(); // Get the WC_Product object
$product_type = $product->get_type();
$product_sku = $product->get_sku();
$product_price = $product->get_price();
$stock_quantity = $product->get_stock_quantity();
}
Try this
function get_names( $order_id ) {
$order = wc_get_order( $order_id );
if (empty($order)) return false;
$items = $order->get_items();
foreach ( $items as $item ) {
$item_name = $item->get_name();
}
return $item_name;
}
Thanks

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) {
// ....
}
}
}

Get category ID from order item inside woocommerce hook

In functions.php we have:
add_action( 'woocommerce_order_status_processing', 'mysite_processing', 10, 1);
then inside this function we get order items:
function mysite_processing($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
}
}
After getting the product id, we want to get the category id that product belongs. We already try with get_the_terms and has_term functions, both didn't work.
Solved, added to items for: $product = wc_get_product( $product_id );
Try below code
function mysite_processing($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$term_list = wp_get_post_terms($product_id,'product_cat',array('fields'=>'ids'));
print_r($term_list);
$cat_id = (int) $term_list[0];
echo $cat_id;
die();
}
}

How to get categories from an order at checkout in WooCommerce?

I am want to get the category of the items in the cart at the checkout in WooCommerce. I want to extract it and then place it in a field in my custom checkout.
I'm using WooCommerce MultiStep Checkout Wizard premium plugin and a specific hook:
add_action('woocommerce_multistep_checkout_before_order_info', 'destinationStep');
I'm a little lost and can't find much documentation for what I need to use to get it.
I'm trying to just get items to appear but I just get an empty array.
$order = new WC_Order( $order_id );
$items = $order->get_items();
var_dump($items);
You could try first with your approach "new WC_Order( $order_id );", this way:
function destinationStep( $order_id )
global $woocommerce;
$order = new WC_Order( $order_id );
$items = $order->get_items();
// echo var_dump($items);
//----
foreach ($items as $key => $item) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$terms = get_the_terms( $product_id, 'product_cat' );
// echo var_dump($terms);
foreach ( $terms as $term ) {
// Categories by slug
$product_cat_slug= $term->slug;
}
}
add_action('woocommerce_multistep_checkout_before_order_info', 'destinationStep', 10, 1);
If it still doesn't work try with "new WC_Order($post->ID)" approach:
function destinationStep()
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$items = $order->get_items();
// echo var_dump($items);
//----
foreach ($items as $key => $item) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$terms = get_the_terms( $product_id, 'product_cat' );
// echo var_dump($terms);
foreach ( $terms as $term ) {
// Categories by slug
$product_cat_slug= $term->slug;
}
}
add_action('woocommerce_multistep_checkout_before_order_info', 'destinationStep');
Update - After some thought:
You can't get the order Id for `'post_type' => 'shop_order', because it doesn't exist yet. This order ID is generated when customer submit the order, but not before on checkout page.
So in this case, it's normal to get an empty array.

Categories