Custom function for post views
-
Hi,
We are using a custom function to update the total_sales every time the product is viewed. When the cache is on the value of total_sales does not update. Wondering if it is possible to update the total_sales post_meta in the below code even when the cache is switched on.
add_action( 'woocommerce_before_single_product', 'prefix_save_product_views' ); function prefix_save_product_views( ) { $product_id = get_the_ID(); $increment = 1; $current_visit_count = get_post_meta( $product_id, 'total_sales', true ); $total_visit_count = (int)$current_visit_count + $increment; update_post_meta( $product_id, 'total_sales', $total_visit_count ); }
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Custom function for post views’ is closed to new replies.