Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Create a category for new items based on “Set Product as New from Date”
 
niceoutfit
Jr. Member
 
Avatar
Total Posts:  22
Joined:  2008-05-09
Rock HIll, SC
 

My goal is to create a category called “New Items” that pulls product based on the “Set Product as New from Date” and the “Set Product as New to Date”.

I have successfully overwritten the _getProductCollection function and added the logic to do something different if the category is named “new item”. The next step would be to write the query to create a collection based on products that have a date attribute that falls within the active range. This is where I’m having trouble.

Here is the function I have that already is overwritting the current function. Any help on how to create a query for these attributes would be very appreciated.

<?php
class Need_Newarrivals_Block_Newarrivals extends Mage_Catalog_Block_Product_List
{
    
protected function _getProductCollection()  
    
{
       
if (is_null($this->_productCollection)) {
           $curr_category 
Mage::registry('current_category')->getName();
           
$curr_categoryid Mage::registry('current_category')->getId();
           
$curr_category strtolower($curr_category);
           if(
$curr_category == 'new items')
           
{
               
//pull a product collection based on the new date attributes and sort newest to oldest
           
}
           
else
           
{
               $this
->_productCollection parent::_getProductCollection();
           
}
       }
       
return $this->_productCollection;  
    
}
}
?>

You’ll see I have the logic set to do something different to the “new item” category. Just need to know how to pull the right products and set them ass the _productColleciton.

Thanks in advance for any help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
niro
Mentor
 
Avatar
Total Posts:  1609
Joined:  2009-03-04
Latvia, Riga
 

You can download extension http://www.magentocommerce.com/extension/859/catalog-new-items
and get the collection code from there…

Or you can get this code in
app\code\core\Mage\Rss\Block\Catalog\New.php

public function getProductCollection()
    
{
        
if (is_null($this->_productCollection)) {
            $storeId 
$this->_getStoreId();
            
$websiteId Mage::app()->getStore($storeId)->getWebsiteId();
      
            
            
$product Mage::getModel('catalog/product');
            
$todayDate $product->getResource()->formatDate(time(), false);
    
          
$productAttributes array_flip(Mage::getSingleton('catalog/config')->getProductAttributes());
          unset(
$productAttributes['price_type']);
          unset(
$productAttributes['price_view']);
          unset(
$productAttributes['special_price']);
          unset(
$productAttributes['special_from_date']);
          unset(
$productAttributes['special_to_date']);
          
$productAttributes array_flip($productAttributes);
            
            
$products $product->getCollection()
                ->
setStoreId($storeId)
                ->
addStoreFilter()
                ->
addAttributeToFilter('news_from_date', array('date'=>true'to'=> $todayDate))
                ->
addAttributeToFilter(array(array('attribute'=>'news_to_date''date'=>true'from'=>$todayDate), array('attribute'=>'news_to_date''is' => new Zend_Db_Expr('null'))),'','left')
                ->
addAttributeToSort('news_from_date','desc')
                ->
addAttributeToSelect($productAttributes'left')
                ->
addAttributeToSelect(array('special_price''special_from_date''special_to_date'), 'left')
            ;
    
            
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
            
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
            
            
$this->_productCollection $products;
        
}
        
        
return $this->_productCollection;
    
}

if you want to use user definet sort order - delete row

->addAttributeToSort('news_from_date','desc')
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top