Skip Navigation

[Resolved] Display Order Products and Quantities

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a View of WooCommerce Orders that shows the products in each order. I would also like to show the quantity of each product.

Solution: At this time, there isn't a built-in way to do this, it's only possible to display each Product from the Order. I have a custom shortcode here that can help. Add this to your child theme's functions.php file:

function order_product_qty_func($atts) {
  $a = shortcode_atts( array(
      'orderid' => '',
      'productid' => ''
  ), $atts );
 
  // get the order using the orderid shortcode attribute
  $order = wc_get_order( $a['orderid'] );
 
  // loop over the order items
  foreach ($order->get_items() as $item_id => $item_data) {
 
    // Get an instance of corresponding the WC_Product object
    $product = $item_data->get_product();
    $product_id = $product->get_id(); // Get the product id
 
    // if the product id matches the current product id in the loop, return the qty
    if( $product_id == $a['productid'] )
      return $item_data->get_quantity();
  }
  return;
}
 
add_shortcode( 'order_product_qty', 'order_product_qty_func');

In the View of Ordered Products, add the Order ID to the View shortcode as an attribute like this:

[wpv-view name="List of Ordered Products" ids="[wpv-ordered-product-ids]" orderid="[wpv-post-id]"]

Then in the Loop Output of the Ordered Products View, you can access the orderid and include the new custom shortcode like this:

[order_product_qty productid="[wpv-post-id]" orderid="[wpv-attribute name='orderid']"][/order_product_qty]

Relevant Documentation:
https://toolset.com/learn/create-an-ecommerce-wordpress-site/displaying-more-information-from-woocommerce/how-to-display-woocommerce-orders-on-the-front-end/

This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 3 voices.

Last updated by calvinS 6 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#662685
Screen Shot 2018-04-14 at 12.24.37 PM.png

Hi,

We would like to display Woocommerce Order Number, Shipping Address and Ordered Products Quantity but cannot seem to locate the shipping address even under "Front-End Content" hidden custom fields.

We followed your tutorial on https://toolset.com/learn/create-an-ecommerce-wordpress-site/displaying-more-information-from-woocommerce/how-to-display-woocommerce-orders-on-the-front-end/ and able to list the ordered products but not the quantity.

We used Post Title and it is not showing the Woocommerce Order Number.

How can we do that?
Looking forward to your reply.
Thanks.

#663114

Managed to figure out the shipping address and and order number but not the ordered products quantity.

Which hidden fields to show ordered products quantity?
Thanks!

#664743

https://toolset.com/learn/create-an-ecommerce-wordpress-site/displaying-more-information-from-woocommerce/how-to-display-woocommerce-orders-on-the-front-end/#how-to-display-ordered-products-when-listing-orders

This is the process to follow when you as well want to involve the actual products in your lists (whether you just display a count or the actual results, is a matter of what you display, not how)

Hence you nee a second View as shown in above-linked DOC.
Instead of outputting the actual Product Data in it's Loop as shown in that DOC, you would use this ShortCode:
https://toolset.com/documentation/user-guides/views-shortcodes/#vf-155378

This will output the number of posts found by the View, and hence, the number of Products each order.

#666240

Thanks for replying. Not sure if I followed you correctly.
I have two views just as how the first tutorial taught.

On the second view - "List all ordered products", I have added "Post Titles" the fields to include in the loop to show which product. And with the shortcode [wpv-post-title]([wpv-found-count]) under the template for this view which outputs 1 only.

I tried to add under the first View (Parent) at templates for this view, with <td>[wpv-view name="List of Ordered Products" ids="[wpv-ordered-product-ids]"]x[wpv-found-count]</td> it is showing 1 also whereas the the actual quantity of the item in the order is 5.

Tried both [wpv-items-count] & [wpv-found-count] but they are showing the same amount.

Thanks for supporting.

#668244

Hi, it sounds like you would like to display the quantity of each Product in the Order. At this time, there isn't a built-in way to do this, it's only possible to display each Product from the Order. I have a custom shortcode here that can help. Add this to your child theme's functions.php file:

function order_product_qty_func($atts) {
  $a = shortcode_atts( array(
      'orderid' => '',
      'productid' => ''
  ), $atts );

  // get the order using the orderid shortcode attribute
  $order = wc_get_order( $a['orderid'] );

  // loop over the order items
  foreach ($order->get_items() as $item_id => $item_data) {

    // Get an instance of corresponding the WC_Product object
    $product = $item_data->get_product();
    $product_id = $product->get_id(); // Get the product id

    // if the product id matches the current product id in the loop, return the qty
    if( $product_id == $a['productid'] )
      return $item_data->get_quantity();
  }
  return;
}

add_shortcode( 'order_product_qty', 'order_product_qty_func');

In the View of Ordered Products, add the Order ID to the View shortcode as an attribute like this:

[wpv-view name="List of Ordered Products" ids="[wpv-ordered-product-ids]" orderid="[wpv-post-id]"]

Then in the Loop Output of the Ordered Products View, you can access the orderid and include the new custom shortcode like this:

[order_product_qty productid="[wpv-post-id]" orderid="[wpv-attribute name='orderid']"][/order_product_qty]
#677612

Thank you! It works!

#747025

Hi, we came across another issue that the shortcode only applicable for simple product.

The quantity would automatically be hidden if the product is a variable product.

Kindly advise how can I prevent that from happening?

Thank you!