Create order details shortcode for WooCommerce - php

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

Related

Add order items in customer notes field

I am trying to add ordered products (quantity per product per line) in customer added notes field for new WooCommerce orders. I introduced the following code in my functions.php but it was not working.
/* Add ordered products to the customer notes field on order creation. */
add_action( ‘woocommerce_new_order’, ‘products_in_customer_notes’, 10, 1 );
function products_in_customer_notes( $order_id ) {
$order = wc_get_order( $order_id );
// Verify it's a WC Order
if ( is_a( $order, 'WC_Order' ) ) {
$customer_note = $order->get_customer_note();
foreach ( $order->get_items() as $item_id => $item ) {
$product_name = $item->get_name();
$quantity = $item->get_quantity();
$customer_note .= "<br>";
$customer_note .= $quantity . 'x ' . $product_name;
}
// Add the note
$order->set_customer_note($customer_note);
// $order->add_order_note($customer_note);
// Save the data
$order->save();
}
}
When that didn't work, I tried to simplify my code by hardcoding a comment instead, but that didn't work either.
/* Add ordered products to the customer notes field on order creation. */
add_action( ‘woocommerce_new_order’, ‘products_in_customer_notes’, 10, 1 );
function products_in_customer_notes( $order_id ) {
$order = wc_get_order( $order_id );
// Verify it's a WC Order
if ( is_a( $order, 'WC_Order' ) ) {
$customer_note = $order->get_customer_note();
$customer_note .= '<br>';
$customer_note .= 'Hardcoded Test';
// foreach ( $order->get_items() as $item_id => $item ) {
// $product_name = $item->get_name();
// $quantity = $item->get_quantity();
// $customer_note .= "<br>";
// $customer_note .= $quantity . 'x ' . $product_name;
// }
// Add the note
$order->set_customer_note($customer_note);
// $order->add_order_note($customer_note);
// Save the data
$order->save();
}
}
What is it that I am missing that is preventing this from working?
The desired output would be something like the following:
Any customer notes if they were inserted at the time of order placement.
1x product A
3x product B
8x product C
etc.
add_action( 'woocommerce_new_order', 'products_in_customer_notes', 10, 2 );
function products_in_customer_notes( $order_id, $order ) {
// Verify it's a WC Order
if ( is_a( $order, 'WC_Order' ) ) {
$customer_note = $order->get_customer_note();
foreach ( $order->get_items() as $item_id => $item ) {
$product_name = $item->get_name();
$quantity = $item->get_quantity();
$customer_note .= "<br>";
$customer_note .= $quantity . 'x ' . $product_name;
}
// Add the note
$order->set_customer_note($customer_note);
// Save the data
$order->save();
}
}
You were missing the parameter count(2), $order_id, $order

How to concatenate the name of a product with the name of its attributes after "woocommerce_thankyou" hook fires?

$order_items = $order->get_items();
foreach ($order_items as $item_key => $item) {
$product = $item->get_product(); // Get the WC_Product Object
if( $product->is_type( 'variation' ) ){
$attributes = $product->get_attributes();
$variation_names = array();
if( $attributes ){
foreach ( $attributes as $key => $value) {
$variation_key = end(explode('-', $key));
$variation_names[] = ucfirst($variation_key) .' : '. $value;
}
}
echo implode( '<br>', $variation_names );
}
}
The output of this code is as follows:
After echo:
%da%86%d8%b1%d8%a8%db%8c
and Incompatible with Persian language
I need the end result to be something like this:
"product Name , color:red, size:85"
.
.
.
.
.
.
.
.
.
.
Thanks to all the friends;
This is my new solution and it has been tested and it works:
Note:attributes $value label added:
//اضافه کردن نام ویژگی به اسم محصول
//Add attribute label name to product name
if( $product->is_type( 'variation' ) ){
$s ='';
$s1 = '';
foreach ($product->get_attributes() as $taxonomy => $attribute_obj ) {
// Get the attribute label
$attribute_label_name = wc_attribute_label($taxonomy);
//convert to array
$attribute_arr = json_decode(json_encode($attribute_obj),true);
$term_name = get_term_by( 'slug', $attribute_arr, $taxonomy)->name;
$s = $s . $s1 .$attribute_label_name.':'.$term_name;
$s1 = ', ';
}
$name = $name . '(' .$s. ')';
echo '<p>' . $name. '</p>';
}
You can use wc_attribute_label() to get the proper label name of the attribute and you can use get_title() to get the product title. try the below code.
function display_attributes_and_name( $order_id ){
$order = wc_get_order($order_id); //<--check this line
$order_items = $order->get_items();
foreach ($order_items as $item_key => $item) {
$product = $item->get_product(); // Get the WC_Product Object
if( $product->is_type( 'variation' ) ){
$attributes = $product->get_attributes();
$variation_names = array();
if( $attributes ){
foreach ( $attributes as $key => $value) {
$variation_key = end(explode('-', $key));
$variation_names[] = wc_attribute_label( $variation_key ) .' : '. $value;
}
}
echo $product->get_title().', '.implode( ',', $variation_names );
}
}
}
add_action( 'woocommerce_thankyou', 'display_attributes_and_name', 10, 1 );
Tested and works
What #Bhautik said
Plus
"Incompatible with Persian language"
To me, it looks like it's been encoded (by your browser perhaps!), so in order to decode it, we could use urldecode function, like so:
$test = '%da%86%d8%b1%d8%a8%db%8c';
$decode_test = urldecode($test);
echo $decode_test;
Which will output this:
چربی
Does that make sense?

Hook JS into WooCommerce Email function

I am trying to add some JS for Trustpilot in my WooCommerce processing order emails (customer-processing-order.php). I am currently trying to just echo it with a function in functions.php like so:
add_action ('woocommerce_email_header', 'add_trustpilot_script', 9999, 3);
function add_trustpilot_script ($headers, $email_id, $order ){
$theSku = '';
foreach ( $order->get_items() as $item_id => $item ) {
$sku = $item->get_sku();
$theSku .= $sku . ',';
}
$tSku = substr($theSku, 0,-1);
echo '<script type="application/json+trustpilot">
{
"recipientName": "'. $order->get_billing_first_name().'",
"recipientEmail": "'. $order->get_billing_email().'",
"referenceId": "'. $order->get_id().'",
"productSkus": ["'.$tSku.'"]
}
</script>';
}
But the above is not working :( any help appreciated.
Your code contains some minor errors
$order is not an argument in the woocommerce_email_header hook
$email->id is used in an if condition, to target the customer_processing_order email
$item->get_sku(); is replaced by $product->get_sku();
So you get:
function action_woocommerce_email_header( $email_heading, $email ) {
// Only for order processing email
if ( $email->id == 'customer_processing_order' ) {
// Get an instance of the WC_Order object
$order = $email->object;
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Empty string
$theSku = '';
foreach ( $order->get_items() as $item_id => $item ) {
// Get an instance of corresponding the WC_Product object
$product = $item->get_product();
// Get product SKU
$product_sku = $product->get_sku();
$theSku .= $product_sku . ',';
}
$tSku = substr( $theSku, 0, -1 );
echo '<script type="application/json+trustpilot">
{
"recipientName": "' . $order->get_billing_first_name() . '",
"recipientEmail": "' . $order->get_billing_email() . '",
"referenceId": "' . $order->get_id() . '",
"productSkus": ["' . $tSku . '"]
}
</script>';
}
}
}
add_action( 'woocommerce_email_header', 'action_woocommerce_email_header', 10, 2 );
Note: the results can be found in the email source, one email client displays the results visually, the other does not, so it depends on which email client you use

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 line item woocommerce order

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

Categories