0

how to sort New Products at Home by Product Name ?

enter image description here

because I see to put it through CMS page in the Admin:

Admin - CMS Pages - home - Content :

{{widget type="catalog/product_widget_new" display_type="new_products" products_count="4" column_count="4" template="catalog/product/widget/new/content/new_grid.phtml"}}
2

Take class Mage_Catalog_Block_Product_Widget_New in local folder or extend it with your own

Find the below function

protected function _getProductCollection()
    {
        switch ($this->getDisplayType()) {
            case self::DISPLAY_TYPE_NEW_PRODUCTS:
                $collection = parent::_getProductCollection();
                break;
            default:
                $collection = $this->_getRecentlyAddedProductsCollection();
                break;
        }

        return $collection;
    }

and replace with this

protected function _getProductCollection()
    {
        switch ($this->getDisplayType()) {
            case self::DISPLAY_TYPE_NEW_PRODUCTS:
                $collection = parent::_getProductCollection();
                break;
            default:
                $collection = $this->_getRecentlyAddedProductsCollection();
                break;
        }
        $collection->addAttributeToSort('name');
        return $collection;
    }
  • Thank you Akhilesh. It works. And only my curiosity, to display only 4 or 5 product, through Home content in the admin simply set products_count with 4 or 5 -> products_count="5" or products_count="4". Whether to sort (like sort by Product Name) also through the content in the admin? – Andhi Irawan Nov 2 '15 at 6:22
  • 1
    This is not the right way of overriding a function, you're basically copying an entire class. Using local is a very ugly method, please follow this guide instead: inchoo.net/magento/… – Mayers Nov 2 '15 at 11:41
  • I already ask to extend in my question – Akhilesh Patel Nov 2 '15 at 11:52
2

Please update bellow function from:

protected function _getProductCollection()
    {
        switch ($this->getDisplayType()) {
            case self::DISPLAY_TYPE_NEW_PRODUCTS:
                $collection = parent::_getProductCollection();
                break;
            default:
                $collection = $this->_getRecentlyAddedProductsCollection();
                break;
        }
        return $collection;
    }

To

protected function _getProductCollection()
{
    switch ($this->getDisplayType()) {
        case self::DISPLAY_TYPE_NEW_PRODUCTS:
            $collection = parent::_getProductCollection();
           $collection->addAttributeToSort('name', 'ASC');
            //or $collection->addAttributeToSort('name', 'desc'); 
            break;
        default:
            $collection = $this->_getRecentlyAddedProductsCollection();
            break;
    }
    return $collection;
  • Thank you Abdul. It works. And only my curiosity, to display only 4 or 5 product, through Home content in the admin simply set products_count with 4 or 5 -> products_count="5" or products_count="4". Whether to sort (like sort by Product Name) also through the content in the admin? – Andhi Irawan Nov 2 '15 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.