Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design
Showing posts with label Product listing. Show all posts
Showing posts with label Product listing. Show all posts

How to Display Random Product list on category/Product listing page in Magento

If you want to display or want to change your default product listing with random listing, just change following simple code.

The product listing page has already category filter applied, check the code at app/code/core/Mage/Catalog/Block/Product/List.php, you simply need to add the random filter there.

It is best practice to override your core file in your local directory instead of changing in core file. So copy file
from app/code/core/Mage/Catalog/Block/Product/List.php to app/code/local/Mage/Catalog/Block/Product/List.php

Now open this file Search _getProductCollection() function. In this function find out
$this->_productCollection = $layer->getProductCollection(); and comment this line and add following code just after this line...

$collection = $layer->getProductCollection();
$collection->getSelect()->order('rand()');
$this->_productCollection = $collection;

Now clear your cache and you are done.

Add "New" product filter option in Product listing Magento


For this open the file Mage_Catalog_Model_Config and we need to make changes to the function getAttributeUsedForSortByArray().


public function getAttributeUsedForSortByArray()
   {
       $options = array(
           'position'  => Mage::helper('catalog')->__('Position'),
           'created_at'  => Mage::helper('catalog')->__('New')
       );
       foreach ($this->getAttributesUsedForSortBy() as $attribute) {
           /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
           $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
       }

       return $options;
   }

This will sort the product by it's created date.

You need to override this class to make the changes. For that you need to do following.
In your Module etc/config.xml you need to add following code.


<models>
            <catalog>
                <rewrite>
                    <config>Company_Test_Model_Config</config>
                </rewrite>
            </catalog>
</models>

and in your Model/Config.php


<?php
class Company_Test_Model_Config extends Mage_Catalog_Model_Config
{
   public function getAttributeUsedForSortByArray()
    {
        $options = array(
           'position'  => Mage::helper('catalog')->__('Position'),
           'created_at'  => Mage::helper('catalog')->__('New')
       );
       foreach ($this->getAttributesUsedForSortBy() as $attribute) {
           /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
           $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
       }

       return $options;
    }
}

Hit like or leave comment if post help!

 

Copyright @ 2017 HKBlog.