Sorting Products by Popularity (Most Sold) on Magento Product Listing Page
As an experienced developer working with e-commerce platforms like Magento, I often encounter questions about sorting products based on sales or popularity. In this post, we’ll explore how to achieve this for your product listing page in Magento.
Understanding the Current Approach
The tutorial you mentioned is a good starting point for sorting products by the number of reviews they have received. However, if you want to sort products by their actual sales volume across different categories, we need to modify our approach slightly.
The code snippet from the collection.php file demonstrates how to join the review table with the main product table and order by the count of reviews:
public function sortByReview($dir){
$table = $this->getTable('review/review');
$entity_code_id = Mage::getModel('review/review')->getEntityIdByCode(Mage_Rating_Model_Rating::ENTITY_PRODUCT_CODE);
$cond = $this->getConnection()->quoteInto('t2.entity_pk_value = e.entity_id and ','').$this->getConnection()->quoteInto('t2.entity_id = ? ',$entity_code_id);
$this->getSelect()->joinLeft(array('t2'=>$table), $cond,array('review' => new Zend_Db_Expr('count(review_id)')))->group('e.entity_id')->order("review $dir");
}
Sorting by Actual Sales Volume
To sort products based on their sales volume across different categories, we need to consider the order history. Magento stores all orders and associated product information in its database.
Here’s a step-by-step guide:
- Retrieve All Orders: First, get a collection of all orders from the
sales_flat_ordertable.
$orders = Mage::getModel('sales/order')->getCollection();
-
Calculate Sales per Product: Iterate through each order and sum up the quantity for each product associated with that order.
-
Order Products by Total Quantity: Sort your product collection based on the total quantity sold across all orders.
// Assuming $product is your product object
$soldQuantity = 0;
foreach ($orders as $order) {
foreach ($order->getAllVisibleItems() as $item) {
if ($item->getProductId() == $product->getId()) {
$soldQuantity += $item->getQtyOrdered();
}
}
}
// Add the sold quantity to your product object or collection
$product->setSoldQuantity($soldQuantity);
- Display Products on Listing Page: In your listing page template, you can now sort and display products based on their
sold_quantityattribute.
<?php foreach ($this->getLoadedProductCollection() as $_product): ?>
<div class="product-item">
<?php if ($_product->getSoldQuantity() > 0): ?>
<span class="popular-label">Most Sold: <?php echo $_product->getSoldQuantity(); ?></span>
<?php endif; ?>
<!-- Your product listing HTML -->
</div>
<?php endforeach; ?>
Free Extensions for Easy Implementation
If you prefer a more automated approach, there are several free extensions available that can help you sort products by popularity:
-
Sort Products by Popularity - This extension allows sorting products based on the number of views or orders.
-
Most Sold Products by Category - This extension displays the most sold products for each category in a widget.
Remember, always ensure you have proper permission to access and modify Magento’s core files and database tables. It’s also recommended to backup your data before making any changes.
In conclusion, sorting products by popularity (most sold) on the Magento product listing page requires a combination of custom code to calculate sales volume from the order history and displaying this information in your templates. With proper implementation and free extensions available for ease, you can enhance your store’s user experience by highlighting popular products across all categories.
Happy coding!