Skip to content
coolplugins-animated-logo
My Account
$0 0 Cart

Custom Product View Counter for WooCommerce

custom product view counter
Custom Product View Counter

In this tutorial, you will learn how to create a Custom Product View Counter for WooCommerce.

Let’s get started.

Need of Product View Counter

It is a good idea to show users how many other users are viewing the product. The product page view counter tells about the total page views of a specific product page, and on every new page view, this counter is incremented.

This counter will auto-increment one per visitor and show you the total views of that page. The page views can be counted based on the visitor’s IP address. If a user visits your page multiple times with the same device, it will be considered as only one. It will only increment if the user visits from a different device.

It gives a great impression on users when they see that this product has many views and they will go for it.

Let’s do this with a couple of lines of code.

Create a WooCommerce Product View Counter:

To create a custom WooCommerce product view counter, you need to make a hook function. Hook function is used to add a post meta field in the wp_postmeta table

While loading the product page template, we will use the wp_add_action() hook to execute that function.

Use the following function hook:

  1. To get the post meta field get_post_meta()
  2. To update the post meta field update_post_meta()
  3. To show counter on product page woocommerce_before_add_to_cart_button
  4. To show counter on shop page woocommerce_after_shop_loop_item

Let’s see the code below.

Single Product Page view counter:

add_action('wp', 'product_view_counter');
function product_view_counter() { 
global $post;
 if ( is_product() ){
     $meta = get_post_meta( $post->ID, '_total_views_count', TRUE );
     $meta = ($meta) ? $meta + 1 : 1; 
     update_post_meta( $post->ID, '_total_views_count', $meta );
 }
}

Add the above code to your wordpress theme’s functions.php file.

This code will check if the page is a product page or not. If it is a product page then a new post meta field will be added ‘_total_views_count‘ against the specific product. It will auto-increment when user visits the page.

Let’s show it on the product page.


add_action( 'woocommerce_before_add_to_cart_button', 'show_product_view_counter_on_product_page', 10);
function show_product_view_counter_on_product_page() {
global $product;
$id = $product->id;         
$meta = get_post_meta( $id, '_total_views_count', true);
if(!$meta) {
    $count = 0;
} else {        
    $count = $meta; 
}       
echo "<div class="custom-visitor-count"><i class="fa fa-eye"></i><span class="counter-value">".$count." Views</span></div>";
}

The code listed above will display the total number of page views for a specific product page. You can select the specific location to show this count, you just need to find the hooks for a particular place.

Ip Address based Product View Counter:

You can add a view counter based on IP address, so one device gets only one increment on the counter. It will be similar hooks and functions as the above code. The only change we have to do is to detect the user’s IP address.

Let’s see all code together based on IP address.

add_action('wp', 'product_view_counter');
function product_view_counter() {
global $post;
$userip = $_SERVER['REMOTE_ADDR'];
if ( is_product() ){  
 $meta = get_post_meta( $post->ID, '_total_views_count', TRUE );

     $meta = (!$meta) ? array() : explode( ',', $meta );

     $meta = array_filter( array_unique( $meta ) );

     if( ! in_array( $userip, $meta ) ) {

           array_push( $meta, $userip );
           update_post_meta( $post->ID, '_total_views_count', implode(',', $meta) );

     }
}
}

add_action( 'woocommerce_before_add_to_cart_button', 'show_product_view_counter_on_product_page', 10);
function show_product_view_counter_on_product_page() {
global $product;
$id = $product->id;         
$meta = get_post_meta( $id, '_total_views_count', true);
if(!$meta) {
    $count = 0;
} else {        
    $count = count(explode(',',$meta));
}       
echo "<div class="custom-visitor-count"><i class="fa fa-eye"></i><span class="counter-value">".$count." Views</span></div>";
}

add_action( 'woocommerce_after_shop_loop_item', 'show_product_view_counter_on_product_page', 10);
function show_product_view_counter_on_product_page() {
global $product;
$id = $product->id;         
$meta = get_post_meta( $id, '_total_views_count', true);
if(!$meta) {
    $count = 0;
} else {        
    $count = count(explode(',',$meta));
}       
echo "<div class="custom-visitor-count"><i class="fa fa-eye"></i><span class="counter-value">".$count." Views</span></div>";
}

This is all about it. I hope you understand this topic, now create your own custom product view counter for your WooCommerce product page.

Author Info

Picture of admin

admin

Share This Post...
Table of Contents

Add a Comment

Share This Post...

代做工资流水公司武汉公司流水制作荆州企业对公流水报价成都查车贷流水商丘代做收入证明绵阳离职证明公司东莞购房银行流水费用合肥入职工资流水代办威海制作工资流水账单新乡企业对公流水打印镇江代办入职流水烟台查自存流水镇江工资银行流水代办武汉打离职证明廊坊房贷流水多少钱桂林签证流水代办西宁公司流水模板信阳企业对公流水代办德阳房贷银行流水 模板江门车贷工资流水 代开菏泽日常消费流水办理福州工资流水app截图费用保定代开入职工资流水镇江银行流水账单代做孝感流水代开湛江收入证明样本湘潭代做消费贷流水舟山工作收入证明多少钱南通银行流水PS代办重庆对公账户流水代开绵阳做个人流水香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声卫健委通报少年有偿捐血浆16次猝死汪小菲曝离婚始末何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言男子被猫抓伤后确诊“猫抓病”周杰伦一审败诉网易中国拥有亿元资产的家庭达13.3万户315晚会后胖东来又人满为患了高校汽车撞人致3死16伤 司机系学生张家界的山上“长”满了韩国人?张立群任西安交通大学校长手机成瘾是影响睡眠质量重要因素网友洛杉矶偶遇贾玲“重生之我在北大当嫡校长”单亲妈妈陷入热恋 14岁儿子报警倪萍分享减重40斤方法杨倩无缘巴黎奥运考生莫言也上北大硕士复试名单了许家印被限制高消费奥巴马现身唐宁街 黑色着装引猜测专访95后高颜值猪保姆男孩8年未见母亲被告知被遗忘七年后宇文玥被薅头发捞上岸郑州一火锅店爆改成麻辣烫店西双版纳热带植物园回应蜉蝣大爆发沉迷短剧的人就像掉进了杀猪盘当地回应沈阳致3死车祸车主疑毒驾开除党籍5年后 原水城县长再被查凯特王妃现身!外出购物视频曝光初中生遭15人围殴自卫刺伤3人判无罪事业单位女子向同事水杯投不明物质男子被流浪猫绊倒 投喂者赔24万外国人感慨凌晨的中国很安全路边卖淀粉肠阿姨主动出示声明书胖东来员工每周单休无小长假王树国卸任西安交大校长 师生送别小米汽车超级工厂正式揭幕黑马情侣提车了妈妈回应孩子在校撞护栏坠楼校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变老人退休金被冒领16年 金额超20万西藏招商引资投资者子女可当地高考特朗普无法缴纳4.54亿美元罚金浙江一高校内汽车冲撞行人 多人受伤

代做工资流水公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化