How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Display a Product View Counter in WooCommerce Without a Plugin
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Display a Product View Counter in WooCommerce Without a Plugin
Web Development

How to Display a Product View Counter in WooCommerce Without a Plugin

how7o
By how7o
Last updated: May 10, 2026
7 Min Read
WooCommerce product view counter — meta-based counter with increment and display hooks
SHARE

A woocommerce product view counter without a plugin is two small hooks: one to increment a post-meta counter on every product-page render, one to display the current count on the product summary. Works with stock WooCommerce, persists through requests, and gives you a meta_value_num column to sort product listings by popularity. This guide covers the basic setup, cache-plugin compatibility via AJAX, and the filter-out-admin pattern.

Contents
  • TL;DR
  • The increment
  • The display
  • Ordering products by popularity
  • Page caching — switch to AJAX
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on WooCommerce 9.x with WordPress 6.5. Originally published 2023-05-02, rewritten and updated 2026-04-23.

TL;DR

// Increment on product page render
add_action( 'woocommerce_before_single_product', 'how7o_increment_product_view' );
function how7o_increment_product_view() {
    if ( current_user_can( 'edit_posts' ) ) {
        return;  // skip editors/admins
    }

    $product_id = get_the_ID();
    $count      = (int) get_post_meta( $product_id, '_product_view_count', true );
    update_post_meta( $product_id, '_product_view_count', $count + 1 );
}

// Display on the product summary
add_action( 'woocommerce_single_product_summary', 'how7o_display_product_view', 25 );
function how7o_display_product_view() {
    $count = (int) get_post_meta( get_the_ID(), '_product_view_count', true );
    if ( $count > 0 ) {
        printf(
            '<p class="product-view-count">%s</p>',
            esc_html( sprintf( _n( '%d view', '%d views', $count, 'how7o' ), $count ) )
        );
    }
}

The increment

add_action( 'woocommerce_before_single_product', 'how7o_increment_product_view' );

function how7o_increment_product_view() {
    if ( current_user_can( 'edit_posts' ) ) {
        return;
    }

    $product_id = get_the_ID();
    $count      = (int) get_post_meta( $product_id, '_product_view_count', true );
    update_post_meta( $product_id, '_product_view_count', $count + 1 );
}

woocommerce_before_single_product fires once per product-page render. The current_user_can('edit_posts') check skips editors, authors, and admins — their views would inflate counts when they’re editing products. (int) casts the meta value so + 1 works whether the row exists (returns a numeric string) or not (returns '').

woocommerce product view counter — before_single_product increments meta, single_product_summary displays it

The display

add_action( 'woocommerce_single_product_summary', 'how7o_display_product_view', 25 );

function how7o_display_product_view() {
    $count = (int) get_post_meta( get_the_ID(), '_product_view_count', true );

    if ( $count > 0 ) {
        printf(
            '<p class="product-view-count">%s</p>',
            esc_html( sprintf( _n( '%d view', '%d views', $count, 'how7o' ), $count ) )
        );
    }
}

Priority 25 slots the counter between the price (priority 20) and the excerpt (priority 30). Common WooCommerce summary priorities: 5 title, 10 rating, 20 price, 30 excerpt, 40 add-to-cart, 50 meta — pick the slot that matches your layout.

The _n() helper picks the singular/plural form — “1 view” vs “42 views” — using WordPress’s translation-aware pluralization.

Ordering products by popularity

$args = array(
    'post_type'      => 'product',
    'posts_per_page' => 12,
    'meta_key'       => '_product_view_count',
    'orderby'        => 'meta_value_num',
    'order'          => 'DESC',
);

$popular = new WP_Query( $args );

Once you’re collecting view counts, you can surface a “Most Viewed” section on shop pages. See ordering posts by meta value for the full WP_Query pattern (including the EXISTS / NOT EXISTS trick for products that haven’t been viewed yet).

Page caching — switch to AJAX

// Register an AJAX endpoint
add_action( 'wp_ajax_how7o_view_product',        'how7o_ajax_product_view' );
add_action( 'wp_ajax_nopriv_how7o_view_product', 'how7o_ajax_product_view' );

function how7o_ajax_product_view() {
    if ( empty( $_POST['id'] ) ) {
        wp_send_json_error();
    }

    $product_id = absint( $_POST['id'] );
    $count      = (int) get_post_meta( $product_id, '_product_view_count', true );
    update_post_meta( $product_id, '_product_view_count', $count + 1 );

    wp_send_json_success();
}

// Inline JS on the product page
add_action( 'woocommerce_before_single_product', function () {
    $product_id = get_the_ID();
    ?>
    <script>
    (function() {
        fetch('<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', {
            method:  'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body:    'action=how7o_view_product&id=<?php echo (int) $product_id; ?>'
        });
    })();
    </script>
    <?php
} );

Page-cache plugins serve pre-rendered HTML, so woocommerce_before_single_product never fires for cached visitors. The AJAX variant loads the cached page, then the browser fires a POST to admin-ajax.php which increments the counter. The fetch is fire-and-forget — no UI updates needed for the increment itself.

Frequently asked questions

What’s the simplest woocommerce product view counter setup?

Two actions: woocommerce_before_single_product to increment a meta counter, and woocommerce_single_product_summary to display it. Store the count in post meta (_product_view_count) so it persists across requests. No plugin, no extra table — WordPress handles the storage via existing post meta.

Will this run on cached pages?

No — page caching (WP Rocket, W3 Total Cache, LiteSpeed) serves pre-rendered HTML without firing woocommerce_before_single_product, so the counter stops incrementing. For cache-compatible counting, move the increment to an AJAX endpoint that the product page calls after render, or use a JS beacon to admin-ajax.php. Cached pages and in-PHP counters don’t mix.

Should I exclude logged-in admins or bots from the count?

Yes for both, usually. Add if ( current_user_can('edit_posts') ) return; to skip editors and admins (their views would inflate the count when editing). For bots, check $_SERVER['HTTP_USER_AGENT'] against a short blocklist (googlebot, bingbot, yandex) or use a library like jaybizzle/crawler-detect. Purely-public counts without either filter drift far from actual interest.

Does the meta _product_view_count slow down queries?

Not on its own. Every view adds one row to wp_postmeta, and WordPress caches it automatically. Ordering a product list by view count (meta_key => '_product_view_count' + orderby => 'meta_value_num') is fast if the meta_value column is indexed, which it is by default. Problems show up only at million-product scale — for those, move counting to a dedicated table.

Why woocommerce_single_product_summary with priority 25?

That hook fires multiple times during the product summary rendering with different priorities — 5 (title), 10 (rating), 20 (price), 30 (excerpt), 40 (add-to-cart), 50 (meta). Priority 25 slots the view count between price and excerpt, which reads naturally. Pick whichever priority matches where you want the count to appear visually.

Related guides

  • How to Order Posts by Meta Value in WordPress — the sort pattern for a “most viewed” listing.
  • How to Include SKU in WooCommerce Search — another filter-driven product customization.
  • How to Automatically Add a Product to Cart on Visit in WooCommerce — another template_redirect style of product-flow tweak.
  • How to Deregister or Remove a CSS File in WordPress — hook-priority discipline, same family of tweak.

References

WooCommerce single-product hooks reference: woocommerce.com/document/woocommerce-action-hook-reference.

TAGGED:performancephpWooCommercewordpress

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article WooCommerce orders as My Account default — menu filter + redirect How to Display Orders Instead of Dashboard on the WooCommerce My Account Page
Next Article WooCommerce dynamic currency switcher — cookie-stored currency applied via woocommerce_currency filter How to Dynamically Change Currency in WooCommerce
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

Laravel Vite combine CSS — @import chain bundles vendor and app stylesheets
Web Development

How to Compile Multiple CSS into One CSS with Laravel + Vite

7 Min Read
WordPress check if user is logged in with is_user_logged_in()
Web Development

How to Check If a User Is Logged In in WordPress

7 Min Read
Login to Laravel programmatically without a password (Auth::login and loginUsingId)
Web Development

Login to Laravel Programmatically Without a Password (Auth::login & loginUsingId)

4 Min Read
Laravel Eloquent exists method checking if a record exists in a database query
Web Development

How to Check if a Record Exists in Laravel

6 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?