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
Related
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
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' );
I am using WooCommerce with Dokan plugin. I need to convert the string to a number so that I can use it for calculations (this has to be easier than I am making it).
I need to use the line subtotal rather than the product price because the product price is pulling back the lowest variable product price instead of the the option they selected.
I have tried ltrim() and substring(), the number is correct but it's returning it formatted and I can't figure out how to get rid of the currency symbol ($) and make it as a float number.
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$product_id = $cart_item['product_id'];
/*?>Product ID: <?php echo $product_id,"<br>"; */
$quantity=1;
$quantity = $cart_item['quantity'];
?>Quantity: <?php echo $quantity,"<br>";
/* $price = WC()->cart->get_product_price( $product );
?>Total Price 1: <?php echo $price,"<br>"; */
//$price = get_post_meta($cart_item['product_id'] , '_price', true);
$price = WC()->cart->get_product_price( $product );
?>Price: <?php echo $price,"<br>";
$res = ltrim($price,12);
?>Price: <?php echo $res,"<br>";
$item_total = $price * $quantity;
?>Item Total: <?php echo $item_total,"<br>";
$vendor_id = get_post($product_id);
/*?>Vendor ID: <?php echo $vendor_id->post_author,"<br>"; */
$admin_commission = get_user_meta( $vendor_id->post_author, 'dokan_admin_percentage', true );
?>Admin Commission: <?php echo $admin_commission, "<br>";
$commission_amount = number_format(($price) * ( (get_user_meta( $vendor_id->post_author, 'dokan_admin_percentage', true ))/100),2);
$commission_total = ($commission_amount/2);
?>Commission_Total: <?php echo $commission_total,"<br><br>";
$amt_raised_for_cause= $amt_raised_for_cause + $commission_total ;
// Anything related to $product, check $product tutorial
//$meta = wc_get_formatted_cart_item_data( $cart_item );
}
Hope this will helpful, $price get only number value.
$amount = floatval( preg_replace( '#[^\d.]#', '', $price ) );
You are not using correct way to get the raw product price for display (float number). Now in your provided code, $amt_raised_for_cause is not defined…
Try the following revisited code instead:
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data']; // The WC_Product Object
$product_id = $cart_item['product_id']; // The product ID (Or the variable product ID)
$variation_id = $cart_item['variation_id']; // The variation product ID
/*?>
Product ID: <?php echo $product_id,"<br>";
Variation ID: <?php echo $product_id,"<br>";
*/
$quantity = $cart_item['quantity']; // The cart item quantity
?>Quantity: <?php echo $quantity,"<br>";
// Display the formatted price html (for cart item)
$price_html = WC()->cart->get_product_price( $product );
?>Price: <?php echo $price_html,"<br>";
// Get the raw product price for display (including taxes if it's enabled)
if ( WC()->cart->display_prices_including_tax() ) {
$price = wc_get_price_including_tax( $product );
} else {
$price = wc_get_price_excluding_tax( $product );
}
// Display the raw product price
/* ?>Price (raw): <?php echo $price,"<br>"; */
$item_total = $price * $quantity;
?>Item Total: <?php echo $item_total,"<br>";
$vendor_id = get_post($product_id); // The dokan vendor ID
/*?>Vendor ID: <?php echo $vendor_id->post_author,"<br>"; */
$admin_commission = get_user_meta( $vendor_id->post_author, 'dokan_admin_percentage', true );
?>Admin Commission: <?php echo $admin_commission, "<br>";
$commission_amount = number_format( $price * ( $admin_commission / 100 ), 2 );
$commission_total = $commission_amount / 2;
?>Commission_Total: <?php echo $commission_total,"<br><br>";
$amt_raised_for_cause = $amt_raised_for_cause + $commission_total; // <=== $amt_raised_for_cause NOT DEFINED
// Anything related to $product, check $product tutorial
//$meta = wc_get_formatted_cart_item_data( $cart_item );
}
Tested and works
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();
}
}
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'];
}
}