Ecommerce Shopify WordPress Discussion

Update WooCommerce product metadata by visiting single product page

I'm trying to count the number of views of a single product page, but I can't do it. I will be glad of any help in solving my question... add_action('template_redirect', 'update_product_data'); function update_product_data(){ if (class_exists('WooCommerce') && is_product()) { $product = wc_get_product( get_the_ID() ); $product_id = $product->get_id(); $count = (int) $product->get_meta('_click_counter'); $count++; $product->update_meta_data('_click_counter', $count); $product->save(); } }
Based on this answer thread, try the following: // Utility function: Updating product views count function single_product_views_count_update( $product ) { if( ! is_a($product, 'WC_Product') ) return; $count = (int) $product->get_meta('_views_counter'); $product->update_meta_data('_views_counter', $count + 1); $product->save(); } // Count single product views add_action( 'woocommerce_single_product_summary', 'count_single_product_views' ); function count_single_product_views(){ global $product; single_product_views_count_update( $product ); } Code goes in functions.php file of your child theme (or in a plugin). Tested and works. Now if you want to avoid repetitive views count from the same user you should use the following instead: // Force non logged users WC Session cookie add_action( 'woocommerce_init', 'force_non_logged_user_wc_session' ); function force_non_logged_user_wc_session(){ if( is_user_logged_in() || is_admin() ) return; if ( isset(WC()->session) && ! WC()->session->has_session() ) { WC()->session->set_customer_session_cookie( true ); } } // Utility function: Get user viewed products function get_user_viewed_products() { if ( is_user_logged_in() ) { $viewed_products = (array) get_user_meta( get_current_user_id(), 'viewed_products', true); } else { $viewed_products = (array) WC()->session->get('viewed_products'); } return $viewed_products; } // Utility function: Updating user viewed products function update_user_viewed_products( $product_id ) { if ( is_user_logged_in() ) { $user_id = get_current_user_id(); $products = get_user_meta( $user_id, 'viewed_products', true ); if ( empty($products) ) { $products = array(); } $products[] = $product_id; update_user_meta( $user_id, 'viewed_products', $products); } else { $products = WC()->session->get('viewed_products'); if ( empty($products) ) { $products = array(); } $products[] = $product_id; WC()->session->set('viewed_products', $products); } } // Utility function: Updating product views count function single_product_views_count_update( $product ) { if( ! is_a($product, 'WC_Product') ) return; if ( in_array( $product->get_id(), get_user_viewed_products() ) ) { return; } else { update_user_viewed_products( $product->get_id() ); } $count = (int) $product->get_meta('_views_counter'); $product->update_meta_data('_views_counter', $count + 1 ); $product->save(); } // Count single product views add_action( 'woocommerce_single_product_summary', 'count_single_product_views' ); function count_single_product_views(){ global $product; single_product_views_count_update( $product ); } Addition By the OP: Display product views in Admin product list // Add a custom column to Admin product list add_filter('manage_edit-product_columns', 'admin_product_views_counter_column'); function admin_product_views_counter_column($columns) { $columns['views_count'] = __('View', 'woocommerce'); return $columns; } // Display the views counter for each product in the "View" column add_action('manage_product_posts_custom_column', 'admin_product_views_counter_column_content', 10, 2); function admin_product_views_counter_column_content($column, $post_id) { if ($column === 'views_count') { global $product; echo (int) $product->get_meta('_views_counter'); } }

January 7, 2024

Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.