AI Verified

Name

Integrate a 'Sold' Column into the WordPress Dashboard's Product Section

About

Are you interested in knowing the quantity of items you have sold?

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

Add this to Code Snippets

Codevault

WDXTechnologies

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

Our AI bot has checked 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:

01/11/2023

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Potential vulnerability found : Cross Site Scripting
Found on line : 21
Code : echo($units_sold)
Vulnerable line : 21
Code : $units_sold = get_post_meta($post_id, 'total_sales', true));

Found 1 vulnerabilities

Integrate a 'Sold' Column into the WordPress Dashboard's Product Section

 
                    
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}

0

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

5

WooCommerce Show Number Of Units Sold On Product Page

Added: 1 year ago

Last Updated: 2 months ago

Showing the number of product units sold on the single product page is a great way to introduce Fear Of Missing Out (FOMO). You can highlight how many products are sold to let customers know a product...

WooCommerce

AI Verified

1

Woo Change Add to Cart Button Text

Added: 1 year ago

Last Updated: 1 year ago

WooCommerce

AI Verified

0

WooCommerce Add A Fee At Checkout Based On The Postcode Entered

Added: 1 year ago

Last Updated: 1 year ago

You may wish to charge an additional fee based on the user’s postcode. This solution shows how you can add a £5 fee (the currency will be whatever you have set it in your WooCommerce Settings) dependi...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WooCommerce

AI Verified

0

Show 'NEW' Badges for Recently Added Items in WooCommerce

Added: 8 months ago

Last Updated: 8 months ago

This code snippet adds a "NEW!" badge to products in WooCommerce, indicating that they are newly added. The badge is displayed both on the product archive pages and the single product template for a s...

WooCommerce

AI Verified

0

Integrate a 'Sold' Column into the WordPress Dashboard's Product Section

Added: 10 months ago

Last Updated: 10 months ago

Are you interested in knowing the quantity of items you have sold?

WooCommerce

AI Verified

0

Transforming Coupon Codes into Discount Codes in WooCommerce

Added: 9 months ago

Last Updated: 9 months ago

In certain situations, the term "coupon" may be inappropriate. To address this, we've developed several guides suggesting alternative synonyms for "coupon." This specific guide focuses on replacing th...