Sort products by most viewed

amasty product sorting
magento 2 sort products by rating
magento 2 product listing sort by newest
magento 2 sort by most sold
magento 2 sort by best seller in toolbar
magento 2 sort by popularity
amasty sorting magento2
magento 2 sorting extension free

I'm using a plugin that already count product views and store it in a table called 'mwb_wpr_data'.

The query to list the product views is:

SELECT DISTINCT('productid') FROM 'mwb_wpr_data' WHERE 'action' = 'view'

The productid field is a FK to Woocomerce products.

How can I modify the Woocommerce default sorting, so it will display products by order of most views based in the table 'mwb_wpr_data'?

Current code using the plugin Post View Counter:

add_action( 'pre_get_posts', 'my_view_filter' );
function my_view_filter($query){
    if ( 
        $query->is_main_query() && 
        ( $query->is_home() || $query->is_archive() || $query->is_search() )
    ) {
            $query->set('suppress_filters', 'false');
            $query->set('orderby', 'post_views');
            $query->set('order', 'DESC');
    }
}

According to me you have set the correct pre_get_post but problem is in your if condition.

You have set the current_user_can which not correct filter is for every user so if you are not login in with administrator role than your query will not work.

current_user_can('administrator')

Remove this above one from the condition.

add_action( 'pre_get_posts', 'my_view_filter' );
function my_view_filter($query){
    if ($query->is_main_query() && ( $query->is_home() || $query->is_archive() || $query->is_search() )
    ) {
            $query->set('suppress_filters', 'false');
            $query->set('orderby', 'post_views');
            $query->set('order', 'DESC');
    }
}

How to implement sort by "Most viewed" products in Magento 2?, Guide to implement sort by “Most Viewed” products in Magento 2. Step 1: First, we need to create two plugins. Before creating the plugins, we first need to declare the plugin in the di. Step 2: Create the first plugin. [Vendor]\[ModuleName]\Plugin\Model\Config.php. Step 3: Create the second plugin. You can use the Shopify best selling sort within your own store to make it easier for shoppers to find your most popular products. For example, you can add a Sort By menu, like this one, to let your users sort your collections by best sellers. Your best sellers can act as a form of social proof and help your customers make the decision to buy. When a customer is not sure what to purchase, the sort by best selling lets them put your most popular products first.


Ok, so the RIGHT way to do this would be

  1. Change how your post view counts are stored, so that they are stored as postmeta of each product (post) in a field like _post_views_count.

  2. Use meta_key & orderby in wp_query like this

    $args = array(
        'meta_key' => '_post_views_count',
        'orderby' => 'meta_value_num',
        'order' => 'ASC'
    );
    

< EDIT >

IF you want to use the same plugin for recording your product views, I'd set up a function that fires every time a post view is added to the table that updates a postmeta field of the product.

Is it elegant? No.

Will it work? Yes.

Sort products by most viewed, According to me you have set the correct pre_get_post but problem is in your if condition. You have set the current_user_can which not correct  Customers can sort by top rated products; Users can sort by most viewed products; Allows your customers to sort by Whishlist; Store owners can separately display the “Out of Stock” product for customer’s easiness. You can set the time period for the hot products till the desirable time period. Set the time period in days for Most Viewed Products.


Not sure about the plugin you're using (it would be great if you give additional info about it) but I've been using Post Views Counter for years and it works really well.

It even has a query parameter for WP_Query (to query posts by post views):

EDIT: This query parameter won't work without the Post Views Counter plugin as it's not a WordPress default parameter.

More info about this plugin API here: https://dfactory.eu/docs/post-views-counter/developers-api/

$query_args = array(
    'posts_per_page'   => 12,
    'order'            => 'DESC',
    'suppress_filters' => false,  //required param
    'orderby'          => 'post_views',  //required param
);
$query      = new WP_Query( $query_args );

if ( $query->have_posts() ):
    while ( $query->have_posts() ): $query->the_post();

//Post content here
    echo get_the_title();

    endwhile;
endif;

I hope this helps, Cheers!

Sort by most viewed products, I strongly recommend this extension. It's not that expensive and it works great. I've used it for CE and EE both with success. Other than sorting by most viewed,  Amazon Sort is a Chrome extension that helps you sort through those. When you’re shopping on Amazon, it usually makes the most sense to try and find the best product, which means sorting results


According to the plugins documentation you are required to pass the post_type as well in order for this to work, i.e.

$args = array(
    'order' => 'asc',
    'post_type' => 'event',
    // required by PVC
    'suppress_filters' => false,
    'orderby' => 'post_views',
    'fields' => ''
);
$most_viewed = get_posts( $args );

That code is in the post that @Marounm linked to in comment higher up.

Try passing the post_type arg as well i.e.

 $query->set('post_type', 'product');

Change Products Sort Order Into Most Viewed by Default, 0.1 installed and i want to display the products by most viewed and this selection to stay as default if possible. As i see opencart don't have most  1) Simply set any products as most viewed products manually. 2) Facility to display auto,manually and both most viewed products. 3) Effectively display most viewed product in slider or grid layout. 4) Ability to insert most viewed product block via layout, CMS, and widgets. 5) Facility to display all most viewed products or display by categories.


Improved Sorting, Provide customers with multiple options for product sorting - by new products, biggest discounts, highest rating, most viewed items and many  New sorting options in this extension: Best Seller: Automatically calculate sales quantity for all of enabled products (every day). Most Viewed: Automatically calculate viewed data for all of enabled products (every day). Newest: Based on the date product is created.


Improving the Sort Order on Category List Pages, With most viewed products option you enable customers to find out what products are more popular even if there are not very many orders in your  About Best Sellers These lists, updated hourly, contain bestselling items. Here you can discover the best in Amazon Best Sellers, and find the top 100 most popular Amazon .


How to Get Most Viewed Product Collection – Mageplaza, On most websites, visitors can change the sort order in which the products for to point to the fact that content lower on the page gets viewed less by visitors. We use cookies to track the preferences of visitors and to identify popular sections of our website. In this way, we can adapt the content of our website more specifically to your needs and thereby improve what we offer you. By clicking accept you agree to our use of cookies. To learn more about cookies and how to refuse them click "Cookies