A Simple Way to List New Products Added on Any Page

I was trying to think of a simple way to add a block on the bottom of the home page that would list X number of new products recently added without worrying about setting the news_from_date etc. Then it dawned on me!

I didn’t want to go through having to create new php blocks, controllers etc, so I simply created a new template (phtml) file and used the existing processing and collection that’s in place for the sitemap.

STEP 1 : Create a new template file (lets call it ‘recently_added.phtml) in –

app/design/frontend/default/YOURTHEME/template/YOURFOLDER/recently_added.phtml


<?php
/**
*
* @see Mage_Catalog_Block_Seo_Sitemap_
*/
?>
<?php

// uses the catalog/seo_sitemap_product collection
$_productCollection = $this->getCollection()
->setPageSize(15)
->setOrder('created_at', 'DESC');

$num_cols = 3;
$i = 0;

if(is_object($_productCollection) && $_productCollection->count()):
?>
<table width="100%">
<tr>
<?php
foreach ($_productCollection as $_item){
echo $i++%$num_cols==0 ? '</tr><tr>' : '';

//beak into 140 character chunks (this is for 3 columns - full page width)
$name_part = str_split( $this->escapeHtml($_item->name), 140 );

//Optional section - crop the title so it doesn't split an ending word
if(isset($name_part[1]) && (!preg_match('/^\s/', $name_part[1]))) {
//strip off the last partial word from the first chunk
$name_part[0] = preg_replace( '/\s\w+$/', '', $name_part[0] );
}

//This is the 'short name' now
$name = $name_part[0];

echo '<td><a href="'.$this->getItemUrl($_item).'">'.$name.'</td>';
}

?>

</tr>
</table>

<?php endif; ?>

This template file grabs the sitemap collection (which is just name and link) and takes the first 15 skus, sorted by created_at date in descending order – thereby giving you the most recently added items.

You’re now done all the processing and just need to include the product listing block wherever you’d like it displayed.

Here’s an example placement:


{{block type="catalog/seo_sitemap_product" block_id="recently_added" template="YOURFOLDER/recently_added.phtml"}}

You can add whatever titles etc you’d like and what you should end up with is 15 skus (3 columns with 5 in each column) of the newest skus. Adjust the parameters in the phtml file to suite your needs)

Done !! .. No need to set attributes or anything!

 

Leave a Reply