i have WordPress site www.kampungcourse.id to choose language program courses. i want to show how many people have watched particular product . using the post view counter p
Please Replace this function as below to show view on top of page next to rating bar.
add_action( 'woocommerce_before_add_to_cart_button', 'add_content_before_rating_button_func',0 );
function add_content_before_rating_button_func() {
global $product;
$id = $product->id;
$meta = get_post_meta( $id, 'views_count', TRUE );
if(empty($meta))
{
$result = 0;
}
else
{
$result = count(explode(',',$meta));
}
?>
<script>
var html="";
var result = "<?php echo $result ?>";
html += "<div class='custom-visitor-count-st' style='font-size: 20px;'>";
html += "<i class='fa fa-eye'></i>";
html += "<span class='cv-value'>";
html += result;
html += " Views</span></div>";
$(html).insertAfter('.woocommerce-product-rating');
</script>
<?php
}
Insert the below code into function.php file. you can insert view count on table by user IP address.
add_action('wp', function() {
global $post;
$user_ip = $_SERVER['REMOTE_ADDR'];
$meta = get_post_meta( $post->ID, 'views_count', TRUE );
$meta = '' !== $meta ? explode( ',', $meta ) : array();
$meta = array_filter( array_unique( $meta ) );
if( ! in_array( $user_ip, $meta ) ) {
array_push( $meta, $user_ip );
update_post_meta( $post->ID, 'views_count', implode(',', $meta) );
}
});
Display particular product view count before add to cart button.
add_action( 'woocommerce_before_add_to_cart_button', 'add_content_before_addtocart_button_func',0 );
function add_content_before_addtocart_button_func() {
global $product;
$id = $product->id;
$meta = get_post_meta( $id, 'views_count', TRUE );
if(empty($meta))
{
$result = 0;
}
else
{
$result = count(explode(',',$meta));
}
echo "<div class='custom-visitor-count-st' style='font-size: 20px;'>";
echo "<i class='fa fa-eye'></i>";
echo "<span class='cv-value'>";
echo $result;
echo " Views</span></div>";
}
You can use any other woo commerce hook to display view count.