Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Carlo Cannavò

Product Sold / Sales Column

Hi,
i’m trying to add woocoomerce column in product list to show number of sales (number of item purchased) and orders.
but i receive strange data.

1. in some product i have Orders > Sales
example orders 60 – sales 23
if at least one quantity is purchased for each order, I shouldn’t have a number greater than or equal to in sales of >=60 ?

2. with my previous code on file function:

add_action( 'manage_product_posts_custom_column', 'wpcb_render_product_column_total_sales', PHP_INT_MAX );
function wpcb_render_product_column_total_sales( $column ) {
	if ( 'total_sales' === $column ) {
		$total_sales = get_post_meta( get_the_ID(), 'total_sales', true );
		echo $total_sales;
	}
}

/**
 * This is only needed if you want to make the column sortable.
 */
add_filter( 'manage_edit-product_sortable_columns', 'wpcb_sortable_column_total_sales', PHP_INT_MAX );
function wpcb_sortable_column_total_sales( $columns ) {
	$columns['total_sales'] = 'total_sales';
	return $columns;
}

i see that results are not the same.
i have more product sold result with code snippet.

3. is there a way to filter and order these columns, i need to create also a Top Seller Product View sortable.

Thanks

2 years, 4 months ago
Stefan van den Dungen Gronovius
Developer

Thanks for your message. I dove a bit into the code to see what’s happening.

1) Yes, that is indeed what I would expect. But on my local environment I had also some values for sales that were lower than the number of orders found. Looking at the queries there is a slight difference between two columns.
For the Sales column, we query the database and do a join on the post table to check for orders that have the order_complete status. The Orders column doesn’t have a join on the post table and therefore completely skips the order status check.

So to be short: the Sales column only shows the ordered products for completed orders. The Orders column shows only the number of completed orders where the product is ordered. I guess we can make that check in Order optional with a setting so that you have chosen which order statuses to include.

2) To be honest, I don’t know how the value for the meta field in Product is populated, but I would more rely on the query we use than the value in the postmeta. If the number is higher than the number in the column, I guess they only keep track of products that are placed in an order, no matter the order status. If that is what you’re looking for, you can of course use our Custom Field column to display the sales from the post meta.

3) The two columns from our plugin that do the query cannot be sorted or filtered because it would be too heavy on performance. But you could use the Custom Field column and select the products_sold meta key to display the post meta value. In that case, sorting and filtering would work and it will probably be fine for your use case.

2 years, 4 months ago

You must be logged in to reply to this topic.