Monday, May 14, 2012

How to list all special price products according the category

Hi all,
I have a part in my project that i have to show all special price products in a page for different category and i have done it. It\’s very simple just go to your admin and create a static page \"Sale\". Now add
{{block type=\"catalog/product_list\" name=\"home.catalog.product.list\" alias=\"product_homepage\" template=\"catalog/product/sale.phtml\"}}
and save the page.
after that go to your app\\code\\core\\Mage\\Catalog\\Block\\Product and create a file sale.phtml and pest the following code in to your file and run it. 
<?php if(Mage::app()->getRequest()->getParam(\'category_id\')==\'\'){ ?><h2 class=\"subtitle\"><?php echo $this->__(\'Sale\'?></h2><?php
    $category_model 
Mage::getModel(\'catalog/category\'); //get category model
    
$_category $category_model->load(2);  
//$categoryid for which the child categories to be found    
    
$all_child_categories $category_model->getResource()
->getAllChildren($_category);
   
    foreach(
$all_child_categories as $category)
        
{
            
if($category!=2){
                $cur_category 
Mage::getModel(\'catalog/category\')->load($category);
                if(
$cur_category->getIsActive()){  ?>
                    
<a href=\"<?php echo $this->getBaseUrl(\'all-new-arrival\').\'sale/?category_id=\'.$category; ?>\"><img src=\"<?php echo $cur_category->getImageUrl(); ?>\" />
<div><?php echo $cur_category->getName(); ?></div></a>                   
            
<?php    }
            }
        }
?>   
<?php }else{ ?>

<?php
    $todayDate  
Mage::app()->getLocale()->date(
)->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
    
$categoryId Mage::app()->getRequest()->getParam(\'category_id\');
    
$catagory_model Mage::getModel(\'catalog/category\')->load($categoryId); //where $category_id is the id of the category
    
$collection Mage::getResourceModel(\'catalog/product_collection\');
    
$collection->addCategoryFilter($catagory_model); //category filter
    
$collection->addAttributeToFilter(\'status\',1); //only enabled product
    
$collection->addAttributeToFilter(\'special_from_date\'
      array(\'date\' => true\'to\' => $todayDate))
       ->
addAttributeToFilter(\'special_to_date\', array(\'or\'=> 
        array( => array(\'date\' => true\'from\' => $todayDate),
                    
=> array(\'is\' => new Zend_Db_Expr(\'null\')))
                ), 
\'left\');
    
$collection->addAttributeToSelect(array(\'name\',\'url\',\'small_image\',\'price\',\'special_price\')); //add product attribute to be fetched
         
    
$collection->addStoreFilter();   
    if(!empty(
$collection))
    
{ ?>
    
<div class=\"category_products\">
      
<?php  foreach ($collection as $_product):  ?>
          
<div class=\"product\">
            <
a href=\"<?php echo $_product->getProductUrl(); ?>\">
            <
img src=\"<?php echo $this->helper(\'catalog/image\')->init($_product, \'small_image\')->resize(172,235); ?>\" width=\"172\" height=\"235\" />
            <
div class=\"product-name\">
                <
a href=\"<?php echo $_product->getProductUrl() ?>\" >
                
<?php echo substr($_product->getName(),0,40).\' ...\' ?> 
                </
a>
            </
div><br/>
            <
div class=\"price\">
            
<?php echo $this->getPriceHtml($_producttrue?>
            
</div>           
            </
a>
         </
div>  
      
<?php endforeach;   ?>
     
</div>      
 
<?php   }else{ ?>          
        
<class=\"note-msg\"><?php echo \'No products exists\';  ?>  </p>
 
<?php   }  ?>
<?php } ?>
 

1 comment:

  1. Hi,
    This functionality would be great to have in Magento, however I can't make it work on my site. Any idea why? I am not sure if it relates to what theme you apply?

    Regards,
    Lasse

    ReplyDelete