A question that has been popping up quite a lot lately in the WooCommerce support forums is how do I display the total amount of units sold for a product on the product page. In this WooCommerce tutorial I will be showing you how to display this on the product page.
Luckily WooCommerce already takes care of the majority of work for us to do this as it automatically keeps track of the number of units sold for each product for reporting purpose, all we have to do it do retrieve the value and display it on the product page.
To display the number of units sold for a product add the following snippet of code to your theme’s functions.php file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'woocommerce_single_product_summary', 'wc_product_sold_count', 11 ); | |
function wc_product_sold_count() { | |
global $product; | |
$units_sold = get_post_meta( $product->id, 'total_sales', true ); | |
echo '<p>' . sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) . '</p>'; | |
} | |
?> |
This will result in the Units Sold text to display underneath the product title and price
Hi and thanks for the tutorial.
If I wanted to show the number of sales for an individual product? How could I accomplish it?
An example would be to show a shirt on the home page and show the number of sales specific to that shirt. Once a sale is made, it would then automatically pull from the sales quantity of that specific shirt and update is made on the home page.
Thanks in advance for your help.
Hi Alex, I presume you are talking about the archive page, for that you can use the following code
add_action( 'woocommerce_after_shop_loop_item_title', 'wc_product_sold_count', 11 );
function wc_product_sold_count() {
global $product;
$units_sold = get_post_meta( $product->id, 'total_sales', true );
echo '' . sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) . '';
}
How to add this to the single product page and the archive page?
Hi sidown, you can just add
add_action( 'woocommerce_after_shop_loop_item_title', 'wc_product_sold_count' );
to the code in the tutorial to have it add it to the archive pages as well.This is great, but I noticed if you cancel an order, you still get the same unites sold amount. Anyway to fix this?
Hi Tim, yes WooCommerce does not restore stock/sales figures when orders are canceled. I wrote a small extension a while back that will enable it, it does not cover the sale stats but I am sure I can add it in if the requests are there.
http://wordpress.org/plugins/woocommerce-auto-restore-stock/
Hi, Thanks for this – it’s helpful that you confirmed that the report of product sold does not take into account the cancelled orders, even those that have just timed out before the customer had time to pay. Is there anything I can do to get the accurate number of products sold to be reported?
Thanks!
Thats awesome. Just what i needed!!!!
Hi Gerhard and all!!!!!
I think woocommerce is a joke! You guys have serious bugs managing inventory, because of that i can’t trust my inventory or what i actually have sold anymore. I have spent a lot many and time investing in your platform, it’s really a bad experience. If someone it’s trying to have a serious business you can’t count on woocommerce.
Your plugin does not manage the state of the product if it’s a varation and please fix the sold amount. Come on, this should have been implemented in the core of the plugin.
stock status on variations:
do_action( ‘wc_update_product_stock_status’, $_product, ?? );
Restore Sold Amount
$_product->total_sales = $_product->total_sales – $item[‘qty’];
Both not working.
Hi Pipita
I find it kind of cheeky that you are coming onto my blog and then criticizing work I did in my spare time, that I do not get paid for at all, that makes yours and other people’s life easier.
With regards to WooCommerce, please also remember that it is a free plugin, you do not pay anything for it. Yet WooThemes spend their time and money developing and improving it for the public. There is also a large amount of contributors that use their spare time to improve the software. Last time I checked there was absolutely nothing wrong with the inventory management, however if you feel there is an issue then instead of complaining and trying to badmouth software you are getting for free that is actually of very high quality, higher then software you would pay for, log this as an issue on github where the core developers can evaluate if this is in fact a bug.
WooCommerce is definitely not a joke, using phrases like that just to get attention or support is definitely not going to help you at all. You are just taking the community that contributes to WooCommerce in the face, making them less likely to want to help you with anything.
What an ass!
I love Woo, but I must agree that total_sales is not well implemented. You cant rely on a value that tell the product is sold when its not – even refunded the data is incorrect. It seems that total_sales is a last minute Nice thing to play with for front end, but creating a happy widget with this information is bad.
Is there anyone who has a solution for get TRUE sold unit out of the product variable / or tweak the storing process, I would be happy to know.
The value is updated as orders are placed, I will have to check the code but I think it also gets adjusted when orders are refunded.
The problem with getting true data is the queries involved to get the data, if you have lots of order it can slow down your site a lot, that is why we keep track of the sales on the product as orders are placed.
Hi, is there a way to only show this on the backend list of product rather than front end? Thanks a lot!
Hey man,
Thanks for this. Is there a way to make a hook and be able to display the numbers sold of a particular product somewhere else on the site (a sidebar, a page)?
Hm, actually this code broke my website, so I guess I don’t an answer on that. I’m terrible at this!
How could I display sales for a specific product on a completely different page? Eg on homepage showing the total number of sales I’ve had. Would this also be possible to display the total value of those sales?
Hello everyone,
Do you have any solution to get how many items from Variation Products are sold? I can’t find it anywhere.
Thanks a lot.
I added the code as you suggested to the functions.php
Now my site is down!!!!
add_action( ‘woocommerce_single_product_summary’, ‘wc_product_sold_count’, 11 );
function wc_product_sold_count() {
global $product;
$units_sold = get_post_meta( $product->id, ‘total_sales’, true );
echo ” . sprintf( __( ‘Units Sold: %s’, ‘woocommerce’ ), $units_sold ) . ”;
}
Hi,
I’m not sure if you can help, but i have to try. If a product is sold on my website it isn’t visible. You have to click on the product and on the product page you will see: this item isn’t available. How can I fix this?
Thanks in advance
This is amazing thank you.
Question though, is there a way to make the amount sold start at a specific amount? Ex: 17, then it would add on the actual?
This article was a lot of help!
I just have one question. How would I be able to reflect the number of items sold on a product page if an order was canceled. I tried installing your plugin but that didn’t work. I did practice order and then canceled it and the number didn’t reflect the canceled item.
Thanks for your help!
Hello,
This is really useful, thanks for the tutorial
Is it possible to only show the Units sold line when more than 0 units have been sold/
show units when total sold > 0
many thanks, Kev
Hi sir,
Thanks for your time and your support.
Please, I have a question please.
When i delete my orders , and then return to the product page, I am still at i sold x pieces .
How can i make the sold number : zero
Because i delete it from my orders list.
Best regards
A.J.
I mean the following :
I use this snipet to display Number Of Products Sold:
id, ‘total_sales’, true );
echo ” . sprintf( __( ‘Units Sold: %s’, ‘woocommerce’ ), $units_sold ) . ”;
}
?>
But when the order being canceled, it doesn’t change anything. It should show the number before the order was placed. Ex:
Before placing order: 10 after placing order : 15 Cancel order : 15 (it’s still there) but I want it’s 10
I noticed if i cancel an order, i still get the same unites sold amount. Anyway to fix this? Anyone can help me?
I am sorry, I mean :
id, ‘total_sales’, true );
echo ” . sprintf( __( ‘Units Sold: %s’, ‘woocommerce’ ), $units_sold ) . ”;
}
?>
But when the order being canceled, it doesn’t change anything. It should show the number before the order was placed. Ex:
Before placing order: 10 after placing order : 15 Cancel order : 15 (it’s still there) but I want it’s 10
I noticed if i cancel an order, i still get the same unites sold amount. Anyway to fix this? Anyone can help me?
Hi,
I tried the code you mentioned above to show sold count for each product, but its not displayed any where. Is there any specific place for entering this script. I amusing good store theme that contains functions.php.
I added this code at the end of the file. But there is no display. Please tell in brief that where this code is added and where to call this function.
Thank you in advance.
Regards
This works also for pre-orders? I need to encourage clients to reach a minimum before the get the product. Many thanks!
Hello Gerhard,
First thank you for your snippet! It works!
Do you think possible to have this achieve for the pre-sales also?
Many thanks for your help on this!
Sonia
I meant Pre-order!
Thank you again!
Is there a way to apply this to categories in woocommerce?
Thank you very much after I tried a lot of times to search like this function in WP but nothing found.
But I still be not understanding on how to put it specific Product page (not all product !)
Thank you
hi
2 questions
– how to display for only “one product” example: id=243
– how to display only product solds over 10 item?
thanks
Hi I entered the code on my site and it instantly stoped working …..would not get access had to get my hosting company to go in and delete code….reading through I believe it happpened to some one else on the thread aswell …so my advice to anyone intending to use this code is be careful
Cathal, the reason this happened is most probably because you included the opening and closing php tags in your file
<?php
and?>
. If you have an existing file you can omit those, obviously I would recommend you only add code to your site if you are aware of what you are doing.0 Pingbacks
Categories
Top Posts & Pages
Instagram
No Instagram images were found.
Get Email Notifications