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

Redirect product category to page ID

Added: 1 year ago

Last Updated: 1 year ago

Redirect a product category to a page ID

WooCommerce

AI Verified

0

WooCommerce Custom Radio Button Field For Product Editor

Added: 1 year ago

Last Updated: 1 year ago

This guide shows you how to create custom radio buttons in the product editor of WooCommerce. This is perfect if you need to be able to select a pre-defined series of options in the backend and displa...

WooCommerce

AI Verified

0

WordPress Create Custom User Role

Added: 1 year ago

Last Updated: 1 year ago

This code snippet creates a custom role showing every possible default capability.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

31

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: 7 months ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 7 months ago

Last Updated: 3 weeks ago

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