April 24, 2023

Show Sold Product Numbers in WordPress Dashboard with Code Snippets

Play Video

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.

Show Sold Product Numbers in WordPress Dashboard with Code Snippets

Follow these steps

  1. 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.
  2. Adding a New Snippet
    Once the plugin is activated, go to the Snippets section and click on “Add New”.
  3. 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

  4. Pasting the Code
    Paste the copied code into the new snippet editor field.
  5. Saving and Activating the Snippet
    Click on “Save Changes and Activate”. The snippet is now active on your website.
  6. 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:

  1. 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.
  2. 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.
  3. “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.
  4. 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.

Required Resources

WooCommerce Logo
Free Options
Code Snippets Logo
Free Options
Credit to Web Squadron
Web Squadron is a popular YouTube channel dedicated to providing informative tutorials, tips, and insights on web development and design. With a strong focus on WordPress and website builders like Bricks Builder, the channel aims to help users of all skill levels, from beginners to advanced developers, create and optimize their websites. By offering easy-to-understand, step-by-step guidance, Web Squadron provides practical and valuable content.
Visit
Show Sold Product Numbers in WordPress Dashboard with Code Snippets
Welcome back!
Enter your Helwp credentials to sign in.

No Account yet? Sign Up

My Account
Menu
Give Feedback
Describe your feedback *
Rate Helwp
Share
Facebook
Twitter
LinkedIn
Reddit
Email
WhatsApp
Telegram
Pocket
Report
Problem *
Describe the problem
Want us to reply?
Your E-Mail
Affiliate Disclosure

At Helwp, we’re committed to transparency and honesty. Therefore, we want to inform you that some of the links on our website are affiliate links. This means that, at no additional cost to you, we may earn a small commission if you click through and make a purchase.

We only promote products or services that we genuinely believe in. These affiliate commissions help us to maintain the website and continue to provide you with free, high-quality WordPress content.

If you are interested in how you can support us even further, check out our support page.