Display Number of Sales Per Product in WooCommerce

This code enables you to display the number of sales per product on the single product page and shop page archives.

Here’s the code for the single product page which uses a single product page hook :

add_action( 'woocommerce_single_product_summary', 'single_product_sales_count', 15 );
function single_product_sales_count() {

    global $product;

    if ( $product && $product->get_total_sales() > 0 ) {
        echo '<p class="product-sales-count">' . esc_html__( 'Sold:', 'text-domain' ) . ' ' . $product->get_total_sales() . '</p>';
    }
}

Here’s the code to display the number of product sales on the shop page archives for each product :

add_action( 'woocommerce_shop_loop_item_title', 'shop_page_sales_count_in_archive', 5 );
function shop_page_sales_count_in_archive() {

    global $product;

    if ( $product && $product->get_total_sales() > 0 ) {
        echo '<p class="product-sales-count-archive">' . esc_html__( 'Sold:', 'text-domain' ) . ' ' . $product->get_total_sales() . '</p>';
    }
}

Here’s some sample CSS to style and center the output :

.product-sales-count {
    font-size: 14px;
    color: #555;
    margin-top: 5px;
}

.product-sales-count-archive {
    text-align:center;
}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *