Search code examples
phpmagentomagento-1.5

Change Magento New Product New.php to be based off id rather than to & from date


I am wanting to change the way the Magento core file New.php calls new products into a block.

The file is located at app/code/core/Mage/Catalog/Block/Product/New.php - I've copied this file over to the local directory to protect it from updates.

The contents of interest are:

protected function _beforeToHtml()
    {
        $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addStoreFilter()
            ->addAttributeToFilter('news_from_date', array('or'=> array(
                0 => array('date' => true, 'to' => $todayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter('news_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => $todayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter(
                array(
                    array('attribute' => 'news_from_date', 'is'=>new Zend_Db_Expr('not null')),
                    array('attribute' => 'news_to_date', 'is'=>new Zend_Db_Expr('not null'))
                    )
              )
            ->addAttributeToSort('news_from_date', 'desc')
            ->setPageSize($this->getProductsCount())
            ->setCurPage(1)
        ;

        $this->setProductCollection($collection);

        return parent::_beforeToHtml();
    }

The file works by pull the optionals field of New Product From and New Product To date fields from the product admin. This is inconvenient given the size of my catalog and manual management needed to update these fields. So basically I'd like to change the functionality to be get the MAX product id (i.e. the most recent product added) and run through to 100 before that. This will list the 100 most recent products in store.

I've tried something like this but it didn't work.

        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addStoreFilter()
            ->addAttributeToFilter ('entity_id')
            ->addAttributeToSort('entity_id', 'desc')
            ->setPageSize($this->getProductsCount())
            ->setCurPage(1)
        ;

        $this->setProductCollection($collection);

        return parent::_beforeToHtml();
    }

This was just attempting to return products based off the product id (entity_id) but nothing returned (it didn't give any php errors either).


Solution

  • Try (remove ->addAttributeToFilter ('entity_id')... tested on v1.7)

    ....
    $collection = $this->_addProductAttributesAndPrices($collection)
            ->addStoreFilter()
            ->addAttributeToSort('entity_id', 'desc')
            ->setPageSize($this->getProductsCount())
            ->setCurPage(1);
    ...