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 );
}
add_filter( 'woocommerce_catalog_orderby', 'misha_add_custom_sorting_options' );
function misha_add_custom_sorting_options( $options ){
$options['popular'] = 'Beliebteste';
return $options;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'misha_custom_product_sorting' );
function misha_custom_product_sorting( $args ) {
// Nach Aufrufen sortieren
if ( isset( $_GET['orderby'] ) && 'popular' === $_GET['orderby'] ) {
$args['meta_key'] = 'product_visit_count';
$args['orderby'] = 'meta_value_num meta_value';
$args['order'] = 'desc';
}
return $args;
}
Is there a hook like “woocommerce_before_external_link” or something? So i could add it.
]]>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?
]]>1. Store the view counts for different time frames, for each post, as a meta key.
This code can go in your functions.php file. Mind the comments – they will help keep your website performing well.
/* Storing views of different time periods as meta keys */
add_action( 'wpp_post_update_views', 'custom_wpp_update_postviews' );
function custom_wpp_update_postviews($postid) {
// Accuracy:
// 10 = 1 in 10 visits will update view count. (Recommended for high traffic sites.)
// 30 = 30% of visits. (Medium traffic websites)
// 100 = Every visit. Creates many db write operations every request.
$accuracy = 50;
if ( function_exists('wpp_get_views') && (mt_rand(0,100) < $accuracy) ) {
// Remove or comment out lines that you won't be using!!
update_post_meta( $postid, 'views_total', wpp_get_views( $postid ) );
update_post_meta( $postid, 'views_daily', wpp_get_views( $postid, 'daily' ) );
update_post_meta( $postid, 'views_weekly', wpp_get_views( $postid, 'weekly' ) );
update_post_meta( $postid, 'views_monthly', wpp_get_views( $postid, 'monthly' ) );
}
}
—
2. Create a new WP_Query, sort by view counts within the last week
You can change this to all time, daily, or monthly. Just change the meta key. If you want this to affect the main query, it’s better to modify the default query using the pre_get_posts filter.
// Get the 3 top viewed posts for the week.
$args = array(
'post_type' => 'post',
'meta_key' => 'views_weekly',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => '3',
);
$top_posts = new WP_Query($args);
—
3. Populate the meta keys for immediate use
If you enter the code above, you’ll notice #2 doesn’t work. This is because the meta keys for each post aren’t added until the view count updates. To do this, set $accuracy to 100% and view each post manually. This will populate the meta keys.
You only need to do this step once. When you are done, change the $accuracy again.
https://www.ads-software.com/plugins/wordpress-popular-posts/
]]>I’m trying to change the sort-by from “comments” to “views”, and to include “range-all”, where should I change in my script (below)? The below script is created by my ex-programmer but he’s no longer helping me. Can anyone help to advise me?
<div id="popularpost">
<img src="https://xxxxxxx.com/wp-content/themes/premiumpixels/images/popular-post.png" style="padding:0 0 0 25px;"/>
<ul id="popular-comments">
<?php
$pc = new WP_Query('orderby=comment_count&posts_per_page=7'); ?>
<?php while ($pc->have_posts()) : $pc->the_post(); ?>
<li>
<div id="popular-img"><a>" title="<?php the_title(); ?>"><?php the_post_thumbnail(array(10,10)); ?></a></div>
<div id="popular-cmts">
<a>" title="<?php the_title(); ?>" style="font-size:13px;"><?php the_title(); ?></a>
<div class="popcmts">
<?php the_time('F j, Y') ?> . <?php comments_popup_link('No Comments;', '1 Comment', '% Comments'); ?>
</div></li>
https://www.ads-software.com/extend/plugins/wordpress-popular-posts/
]]>