Pro Verified

Name

Add the Sold Column to Products in the Wordpress Dashboard

About

Wouldn't you like to know how many of an item you'd sold?

Language

PHP

Rating

Voted: 4 by 6 user(s)

How to Setup Snippet

Add this to Code Snippets

Codevault

WebSquadron

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

30/04/2024

Important Note

This snippet has the following status:

Pro Verified

This snippet has been verified by a Code Snippet Pro team member.

Pro Comments:

Add the Sold Column to Products in the Wordpress Dashboard

 
                    
1// Add Sold column to Products table
2add_filter('manage_edit-product_columns', 'add_sold_column_to_product_table');
3function add_sold_column_to_product_table($columns) {
4 $columns['product_sold'] = __('Sold', 'text-domain');
5 return $columns;
6}
7 
8// Make Sold column sortable
9add_filter('manage_edit-product_sortable_columns', 'make_sold_column_sortable_in_product_table');
10function make_sold_column_sortable_in_product_table($columns) {
11 $columns['product_sold'] = 'product_sold';
12 return $columns;
13}
14 
15// Populate Sold column with number of units sold
16add_action('manage_product_posts_custom_column', 'populate_sold_column_in_product_table', 10, 2);
17function populate_sold_column_in_product_table($column, $post_id) {
18 if ($column === 'product_sold') {
19 $product = wc_get_product($post_id);
20 if ($product->is_type('simple')) {
21 $units_sold = get_post_meta($post_id, 'total_sales', true);
22 echo $units_sold;
23 } else {
24 echo '-';
25 }
26 }
27}
28 
29// Define custom sorting for Sold column
30add_action('pre_get_posts', 'custom_sorting_for_sold_column');
31function custom_sorting_for_sold_column($query) {
32 global $pagenow;
33 $orderby = $query->get('orderby');
34 if ($pagenow === 'edit.php' && $orderby === 'product_sold') {
35 $query->set('meta_key', 'total_sales');
36 $query->set('orderby', 'meta_value_num');
37 }
38}

4

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

1

WooCommerce Free Shipping For Specific Products

Added: 1 year ago

Last Updated: 2 weeks ago

Firstly, you try to achieve free shipping if a certain product is in the cart by using shipping classes. Only implement this solution if you have exhausted native WooCommerce tools. This solution simp...

WooCommerce

AI Verified

1

WooCommerce Sort Products By SKU

Added: 1 year ago

Last Updated: 2 weeks ago

For very rare cases, you may wish to allow customers to sort products by SKU. This use case would be useful in perhaps a B2B setting, events, or wholesaler.

WooCommerce

AI Verified

1

WooCommerce Change Add To Cart Button Text On Product Archive

Added: 1 year ago

Last Updated: 1 year ago

Like with changing the text on the single product template, it’s important to note that if you’re trying to change the button text to either “add to cart” or “add to basket” then this guide is not for...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

33

Remove Unused Javascript

Added: 1 year ago

Last Updated: 1 day ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

10

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 3 weeks ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 8 months ago

Last Updated: 2 months ago

This can be used for any WordPress Builder to aid working with CSS Grids.