求助:实现WooCommerce商品页面显示Post View Counter浏览量
Got it, let's fix this for your WooCommerce product pages. The Post View Counter plugin typically only tracks default post post types out of the box, which is why it's not working on your product pages (since those are product custom post types). Here are two solid solutions you can implement:
This method tweaks the existing plugin to recognize and track product pages. Add this code to your theme's functions.php file (or a custom plugin for safer long-term use):
// Enable Post View Counter for WooCommerce products function pvc_enable_product_tracking() { // Check if the plugin is active if (!class_exists('Post_View_Counter')) return; // Get the current post types the plugin tracks $tracked_types = Post_View_Counter()->options->get('post_types'); // Add 'product' to the list if it's not already there if (!in_array('product', $tracked_types)) { $tracked_types[] = 'product'; Post_View_Counter()->options->set('post_types', $tracked_types); Post_View_Counter()->options->save(); } } add_action('init', 'pvc_enable_product_tracking');
How to display the view count:
- Use the plugin's built-in shortcode
[post-views]directly in your product description or page builder. - Or add this snippet to your theme's
single-product.phptemplate (or a WooCommerce template override) to place it automatically:echo '<p class="product-view-count">*Total Views: ' . pvc_get_post_views(get_the_ID()) . '*</p>';
If you want full control and avoid relying on the plugin, build a custom view tracker. This is more stable if you're worried about plugin updates breaking things.
Step 1: Add the tracking function
This code counts unique views (using sessions to avoid counting repeat visits from the same user):
// Track unique WooCommerce product views function custom_track_product_views() { // Only run on single product pages if (!is_singular('product')) return; global $post; $product_id = $post->ID; // Skip counting for admins/editors (optional but recommended) if (current_user_can('manage_options')) return; // Check if user already viewed this product in their session $viewed_products = isset($_SESSION['viewed_products']) ? $_SESSION['viewed_products'] : array(); if (!in_array($product_id, $viewed_products)) { // Update the view count in post meta $current_views = get_post_meta($product_id, 'custom_product_views', true); $current_views = $current_views ? intval($current_views) + 1 : 1; update_post_meta($product_id, 'custom_product_views', $current_views); // Mark product as viewed in session $viewed_products[] = $product_id; $_SESSION['viewed_products'] = $viewed_products; } } add_action('wp', 'custom_track_product_views');
Step 2: Add a way to display the count
Choose either option:
Option A: Shortcode for flexible placement
Add this code to create a shortcode you can use anywhere on product pages:
// Shortcode to display product view count function custom_product_view_shortcode($atts) { $args = shortcode_atts(array( 'id' => get_the_ID(), 'label' => 'Total Views: %d' // Customize this text as needed ), $atts); $view_count = get_post_meta($args['id'], 'custom_product_views', true); $view_count = $view_count ? intval($view_count) : 0; return sprintf($args['label'], $view_count); } add_shortcode('product-views', 'custom_product_view_shortcode');
Usage: [product-views label="Course Views: %d"]
Option B: Auto-display on product pages
Add this code to automatically show the count in the product summary (adjust the hook priority to change its position):
// Auto-display view count on single product pages function display_product_view_count() { // Only show on single product pages if (!is_singular('product')) return; // Optional: Restrict to specific product categories (e.g., language courses) $product = wc_get_product(get_the_ID()); if (!$product->has_category('language-courses')) return; $view_count = get_post_meta(get_the_ID(), 'custom_product_views', true); $view_count = $view_count ? intval($view_count) : 0; echo '<p class="product-view-count" style="margin: 10px 0;">*Course Views: ' . $view_count . '*</p>'; } // Hook to display after product title (priority 10 = right after title; 15 = after price) add_action('woocommerce_single_product_summary', 'display_product_view_count', 10);
Key Notes:
- Always back up your site before editing theme files. Using a custom plugin for this code is safer (avoids losing changes when you update your theme).
- If you use a caching plugin, you may need to exclude single product pages from full-page cache to ensure accurate counting. Alternatively, switch to an AJAX-based counting method (more advanced, but avoids caching conflicts).
内容的提问来源于stack exchange,提问作者imran khan

