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

0

WooCommerce Add Content Under Product Prices On Archive Page

Added: 1 year ago

Last Updated: 1 year ago

This quick guide shows you how to add any custom content under the product price on the WooCommerce Product Archive template. You may wish to show a shipping notice, promotion, or in our example “Pric...

WooCommerce

AI Verified

1

Remove Price Sorting Options in WooCommerce Catalog

Added: 4 months ago

Last Updated: 3 weeks ago

This code snippet removes the price sorting options ("Price" and "Price (desc)") from the sorting dropdown menu in the WooCommerce catalog. This can be useful if you want to customize the sorting opti...

WooCommerce

AI Verified

1

WooCommerce Change “$0.00” to “Free”

Added: 1 year ago

Last Updated: 8 months ago

If you have products which are free, the single product template and archive loop item will display the price as “$0.00” by default. This default display could confuse users. A better options would be...

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.