gpt4 book ai didi

Showing Product View Count on Woo commerce Product Page(显示产品查看计数在Woo commerce产品页)

转载 作者:bug小助手 更新时间:2023-10-25 13:35:29 25 4
gpt4 key购买 nike



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 plugin it show post view on home page and blog page but not on woo-commerce product pages. can anyone help with custom code to implement this

我有WordPress网站www.kampengCourse.id来选择语言课程。我想展示一下有多少人看过特定的产品。使用帖子视图计数器插件,它可以在主页和博客页面上显示帖子视图,但在商务产品页面上不会显示。有没有人可以帮助编写定制代码来实现这一点



i tried using post view counter plugin but it does not work on product pages that are part of woo-commerce.

我试过使用POST VIEW计数器插件,但它在属于Woo-Commerce的产品页面上不起作用。


更多回答

Please refer this woo commerce plugin on github for product view count github.com/wp-plugins/product-view-counter

请参考GitHub上的此WOO商务插件以获取产品视图计数githorb.com/wp-plugins/product-view-Counter

优秀答案推荐

Insert the below code into function.php file. you can insert view count on table by user IP address.

将以下代码插入到unction.php文件中。您可以按用户IP地址在表中插入查看计数。



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.

您可以使用任何其他woo商务钩子来显示点击量。



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
}


Here is the updated hook for viewing view count on product page view
Let me know if this helped you out.

以下是更新的钩子,用于查看产品页面视图上的查看计数让我知道这是否对您有帮助。


i will look and make revisions on this one.

我会看一看,并对这一点进行修改。


Please next time do mention which theme you're using it helps to get answer faster.

请下次一定要提到你使用的主题,这有助于更快地得到答案。


add_action('wp', function() { // Adds a function to the 'wp' action hook

global $post; // Retrieves the global $post variable

$user_ip = $_SERVER['REMOTE_ADDR']; // Retrieves the IP address of the user

$meta = get_post_meta( $post->ID, 'views_count', TRUE ); // Retrieves the 'views_count' metadata for the post

$meta = '' !== $meta ? explode( ',', $meta ) : array(); // Splits the metadata into an array using comma as the delimiter, or creates a new array if the metadata is empty

$meta = array_filter( array_unique( $meta ) ); // Removes any duplicate IP addresses from the array

if( ! in_array( $user_ip, $meta ) ) { // Checks if the user's IP address is already in the array

array_push( $meta, $user_ip ); // Appends the user's IP address to the array

update_post_meta( $post->ID, 'views_count', implode(',', $meta) ); // Updates the 'views_count' metadata for the post with the modified array, converting it back to a comma-separated string
}
});

更多回答

will it show total number of product watched by a perticular visitor. or total number of visit by all users. because i want to show total number of visits by all user,. so visitors can know how many times a product has been watched.

它是否会显示一个固定访问者观看的产品总数。或所有用户访问的总次数。因为我想要显示所有用户的访问总数。这样,访问者就可以知道一款产品被观看了多少次。

It will show total count of product watch by all user.

它将显示所有用户观看产品的总数。

so i should add both code to function.php or just the second one

所以我应该将这两个代码都添加到unction.php中,或者只添加第二个代码

Add Both Code into function.php.

将这两个代码添加到unction.php中。

yehh. It will Works. You can Change design According as per your requirement in add_content_before_addtocart_button_func() function.

是啊。它会奏效的。您可以根据需要在Add_Content_Bethe_addtocart_Button_Func()函数中更改设计。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com