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: 3 by 3 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:

05/09/2023

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}

3

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

0

WooCommerce Add Price Suffix

Added: 1 year ago

Last Updated: 1 year ago

This snippet allows you to add custom content as a suffix to WooCommerce prices. You may wish to display information regarding tax, a product’s status, whether it’s used or new etc. The possibilities...

WooCommerce

AI Verified

1

WooCommerce Show Percentage Saving Amount On Sale Product

Added: 1 year ago

Last Updated: 1 year ago

This snippet allows you to display the percentage saving amount on sale products on the single product template and on the loop items.

WooCommerce

AI Verified

0

WooCommerce Product Time Picker

Added: 1 year ago

Last Updated: 1 year ago

This snippet adds a product time picker to the single product template above the add to cart button and save the time picked against the product as meta data. Once a time is picked against the product...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

29

Remove Unused Javascript

Added: 1 year ago

Last Updated: 2 weeks ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

9

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 5 months ago

Deactivate some WooCommerce Checkout Fields from showing

Performance

Pro Verified

5

Disable e-icons from Elementor

Added: 1 year ago

Last Updated: 1 year ago

Disable e-icons - and remove the notification from occurring during your Page Speed Insight Check.