Discover how to show sold product numbers in WordPress with the help of the Code Snippets plugin. Imran from Web Squadron uses the free plugin to quickly and easily display the number of each product sold directly in the WordPress products dashboard. This process is particularly useful for e-commerce websites where tracking product sales is crucial.
Follow these steps
- Installing Code Snippets Plugin
Navigate to your WordPress dashboard and go to the plugins section. Click on “Add New” and search for “Code Snippets”. Install and activate the plugin. - Adding a New Snippet
Once the plugin is activated, go to the Snippets section and click on “Add New”. - Copying the Code
// Add Sold column to Products table add_filter('manage_edit-product_columns', 'add_sold_column_to_product_table'); function add_sold_column_to_product_table($columns) { $columns['product_sold'] = __('Sold', 'text-domain'); return $columns; } // Make Sold column sortable add_filter('manage_edit-product_sortable_columns', 'make_sold_column_sortable_in_product_table'); function make_sold_column_sortable_in_product_table($columns) { $columns['product_sold'] = 'product_sold'; return $columns; } // Populate Sold column with number of units sold add_action('manage_product_posts_custom_column', 'populate_sold_column_in_product_table', 10, 2); function populate_sold_column_in_product_table($column, $post_id) { if ($column === 'product_sold') { $product = wc_get_product($post_id); if ($product->is_type('simple')) { $units_sold = get_post_meta($post_id, 'total_sales', true); echo $units_sold; } else { echo '-'; } } } // Define custom sorting for Sold column add_action('pre_get_posts', 'custom_sorting_for_sold_column'); function custom_sorting_for_sold_column($query) { global $pagenow; $orderby = $query->get('orderby'); if ($pagenow === 'edit.php' && $orderby === 'product_sold') { $query->set('meta_key', 'total_sales'); $query->set('orderby', 'meta_value_num'); } }
Source: Code Snippets
- Pasting the Code
Paste the copied code into the new snippet editor field. - Saving and Activating the Snippet
Click on “Save Changes and Activate”. The snippet is now active on your website. - Checking the Products Dashboard
Go to the products dashboard in WordPress. You should now see a new “Sold” column displaying the number of each product sold. You can also sort products by this column.
Why You Should Show Sold Product Numbers in WordPress
Quick Access to Sales Data
Having the number of each product sold directly in the products dashboard provides quick and easy access to your sales data. This can save you time and effort compared to checking individual product pages or sales reports.
Improved Inventory Management
Knowing how many of each product you’ve sold can help with inventory management. You can quickly see which products are selling well and which aren’t, and adjust your inventory accordingly.
Enhanced Sales Strategy
Sales data can inform your sales strategy. For example, you might decide to run a promotion on a product that’s selling well, or discontinue a product that’s not selling.
Better Customer Understanding
Sales data can also give you insights into your customers’ preferences. You can see which products are most popular with your customers and use this information to improve your product offerings and marketing strategies.
Troubleshooting Common Issues with Code Snippets
While the process of using Code Snippets is generally straightforward, you may encounter some issues along the way. Here are some common problems and their solutions:
- Snippet Doesn’t Work
If your snippet doesn’t work, double-check the code for any syntax errors. Make sure you’ve copied the entire code correctly and haven’t missed any characters. - Snippet Causes Site to Crash
If activating a snippet causes your site to crash, it’s likely that there’s a conflict with another plugin or theme. You can use the “Safe Mode” feature in Code Snippets to deactivate all snippets and regain access to your site. Then, try activating snippets one by one to identify the problematic code. - “Sold” Column Doesn’t Appear
If the “Sold” column doesn’t appear in your products dashboard, make sure the snippet is activated. If it is, check your WooCommerce settings to ensure that product sales are being tracked correctly. - Incorrect Sales Numbers
If the sales numbers in the “Sold” column seem incorrect, it could be due to a caching issue. Try clearing your WordPress cache and refreshing your dashboard. If the numbers still seem off, check your WooCommerce sales reports to ensure they match.
Remember, when troubleshooting, it’s important to make changes one at a time and test after each change. This will help you identify exactly what’s causing the issue and avoid introducing new problems. If you’re still having trouble after trying these solutions, consider reaching out to the Code Snippets support for assistance.