0

I want to show latest added products from all or some specific categories on home page. I'm trying in this way, I have created a block and the block code is here :

 protected function _getProductCollection()
    {
    parent::__construct();
    $storeId    = Mage::app()->getStore()->getId();
    $todayDate  = Mage::app()->getLocale()->date()- >toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

    $products = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToSort("entity_id","DESC")
        ->addAttributeToSelect(array('name', 'price', 'small_image'))
        ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInSiteIds())
        ->setOrder($this->get_order(), $this->get_order_dir());

    if(Mage::getStoreConfig('catalogextensions/config4/max_product'))
    {
        $products->setPageSize(Mage::getStoreConfig('catalogextensions/config4/max_product'));
    }


    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
    Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);

    $this->_productCollection = $products;

    return $this->_productCollection;
}

But it only showing me last category's products. Suppose I have Imported 100 products for a xyz category then it showing me products from xyz category only. But I don't want this. I want to show new products from all or some specific category. Any help will be great appreciable.

1
 protected function _getProductCollection()
    {
        $todayStartOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('00:00:00')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $todayEndOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('23:59:59')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        /** @var $collection Mage_Catalog_Model_Resource_Product_Collection */
        $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' => $todayEndOfDayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter('news_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => $todayStartOfDayDate),
                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)
        ;

        return $collection;
    }

may be this is help ful for you.

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.