Add: Sort Products by Views
-
Hello Guys,
can you pls help me with a Problem. I use Woocommerce for my Affiliate Shop, so People dont buy the products on my site. Which means “Sort by Popularity dont work”. Now i want a Sort Option which Sort my Products after Products views. I already found this here in the internet:
/**
* Setting post count on each visit
*
*/
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, ‘product_visit_count’, true );$total_visit_count = (int)$current_visit_count + $increment;
update_post_meta( $product_id, ‘product_visit_count’, $total_visit_count );}
/**
* Change the display order based on visit count only in Catalog
*
*/
add_filter(‘woocommerce_get_catalog_ordering_args’, ‘prefix_woocommerce_catalog_orderby’);
function prefix_woocommerce_catalog_orderby( $args ) {
$args[‘meta_key’] = ‘product_visit_count’;
$args[‘orderby’] = ‘meta_value_num’;
$args[‘order’] = ‘desc’;
return $args;
}It worked, but it automatically Sort by Products after Views, and dont add an extra Option. Can someone help me to transform this into an option. Thanks.
Or is there any other working method?
- The topic ‘Add: Sort Products by Views’ is closed to new replies.