2
votes

First I would like to say that I am entirely new to Magento so in answering please do not assume any knowledge of Magento - I would however be very greatful to anyone who can point me in the right direction.

The problem we are having is with the new product widget that is placed on the home page - I didnt install it, I didnt set up Magento, I am just making an attempt to fix this for a friend who has been waiting over a year for her developer to produce a site quoted as six weeks, it now just about works.

Magento version is 1.5.1

The problem is that the products in the new products widget are being sorted by news_from_date - now i dont know where magento is pulling this from and it doesnt seem to correspond to the order products are being added in so the plan is to sort by entity_id desc, I have managed to code this into the drop down sort list but I am struggling to achieve this in the new products widget.

I dont fully understand magentos file structure and whilst I would expect this code to be in a widget folder I cannot find it there and trawling google has suggested the file I should be looking at is here :

app/code/core/Mage/Catalog/Block/Product/New.php this file contains the following block of code which I think I should be looking at:

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();
}

Now, I have tried playing with this and changing it in various ways and nothing seems to be changing so I guess I am in the wrong place as I havent even managed to break the widget.

Last thing I tried was this:

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()
        ->addAttributeToSort('entity_id', 'desc')
        ->setPageSize($this->getProductsCount())
        ->setCurPage(1)
    ;

    $this->setProductCollection($collection);

    return parent::_beforeToHtml();
}

....Nothing, no change, nada. Any pointers much appreciated - I think I have thrown myself in at the deep end here and Im drowning in a complex cumbersome system that I dont understand.

Thanks in advance folks

1

1 Answers

2
votes

It should be helpful to know that the news_from_date field is called "Set Product as New from Date" in the admin. It is normally on the "General" tab of a product's edit page (if the previous developer hasn't changed it) and so is an early field to see when creating a product.

The reason it's a manual field is so it can be given a future date or intentionally left blank to exclude it from any new product list. There is also a "Set Product as New to Date" field to limit it's time on lists.

The site's caching ensures that the list doesn't change too often, only once per 24 hours if I recall, which also means that if you change the "Set Product as New from Date" it won't appear to have any effect. You can force the issue by flushing the cache. Your code changes are ineffective for the same reason, you may find it changes tomorrow.


If you're going to change the normal behaviour instead of using the date fields as designed then copy the modified New.php to app/code/local/Mage/Catalog/Block/Product/ so that it won't get overwritten by automated updates. Your edits look valid at first glance, just turn off the cache when experimenting and turn it back on when finished.