Show Magento products by month and year

For the last two days, I have been working on a “gallery like” approach to Magento. One of stores we are developing requires it. Below is a code that lists moths, from January to December, showing number of products for each month. You can reuse function to list products for any given month. Just extract the code from within the function and place it inside some foreach loop and deal with product object. This way you can have one block listing months with appropriately generated links and clicking that link leads you to another page that loads products for given month and year.
<!--?php <br ?-->
/**
* Returnes string (HTML list) of months with total count of producst they hold
*
* @param string $year String representing the year, like '2008' or '2009'
* @return string Returnes string, HTML stuff like</pre>
<ul>
<ul>
<li>...</li>
</ul>
</ul>
<ul>...
<li>...</li>
</ul>
<pre>
*/
/* @var $this Mage_Core_Block_Template */
/*
* Calling from CMS Pages
* {{block type="core/template" template="activecodeline/month_list.phtml"}}
* or something like
* {{block type="core/template" template="activecodeline/month_list.phtml" year="2007" galleryLink="some-url-key"}}
*
* Calling from layout files
*
*
* you can even set some params like
*
*/
?>
<!--?php /** * Check to see if the year has been set on $this object * Enables you to set year from CMS pages or layout files or... */ $year = isset($this--->year)? $this->year : null;
$galleryLink = isset($this->year)? $this->year : $this->getUrl('gallery');
/**
* List displays months as links. You can assing general url part of a path
* but you cannot assign variable names for GET. This function works by sending 2 GET variables
* Those variables are 'month' = some value, and 'year' = some value
*
* @param string $year String representing a year, like '2007'
* @param string $galleryLink Part of a url string, url key of page we wish to go to
* @return string HTML content, ul wrapped by div
*/
function monthsListForCurrentYear($year = null, $galleryLink = null)
{
$monthList = null;
$monthList .= '</pre>
<div>';
$monthList .= '
<ul>
<ul>';</ul>
</ul>
<ul>
<ul>for($i = 1; $i <= 12; $i++) { $date = new Zend_Date(); $date->setMonth($i);</ul>
</ul>
<ul>
<ul>if(is_null($year))</ul>
</ul>
<ul>
<ul>{</ul>
</ul>
<ul>
<ul>$year = $date->getYear()->get('y');</ul>
</ul>
<ul>
<ul>}</ul>
</ul>
<ul>
<ul>$month = $date->getMonth()->get('MMMM');</ul>
</ul>
<ul>
<ul>$rangeFrom = $year.'-'.$date->getMonth()->get('MM').'-01';</ul>
</ul>
<ul>
<ul>/**</ul>
</ul>
<ul>
<ul>* Check out the '31'... does not matter even if it's, let's say '36'</ul>
</ul>
<ul>
<ul>* Result returned is propagastes down to lowest day in month :)</ul>
</ul>
<ul>
<ul>*/</ul>
</ul>
<ul>
<ul>$rangeTo = $year.'-'.$date->getMonth()->get('MM').'-31';</ul>
</ul>
<ul>
<ul>/**</ul>
</ul>
<ul>
<ul>* Get product collection in $rangeFrom - $rangeTo dates</ul>
</ul>
<ul>
<ul>*/</ul>
</ul>
<ul>
<ul>$_productCollection = Mage::getResourceModel('catalog/product_collection');</ul>
</ul>
<ul>
<ul>$_productCollection->addFieldToFilter('created_at', array(array('from' => $rangeFrom)));</ul>
</ul>
<ul>
<ul>$_productCollection->addFieldToFilter('created_at', array(array('to' => $rangeTo)));</ul>
</ul>
<ul>
<ul>$_productCollection->setVisibility(array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH));</ul>
</ul>
<ul>
<ul>$monthList .= '</ul>
</ul>
<ul>
<ul>
<li class="month-name" id="'.strtolower($month).'">';
$monthList .= '<a title="Showcase view" href="'.$galleryLink.'?month='.$date->getMonth()->get('MM').'&year='.$year.'">';
$monthList .= $month.' ('.$_productCollection->count().')';
$monthList .= '</a>';
$monthList .= '</li>
</ul>
</ul>
<ul>
<ul>';</ul>
</ul>
<ul>
<ul>unset($date);</ul>
</ul>
<ul>
<ul>}</ul>
</ul>
<ul>$monthList .= '</ul>
';
$monthList .= '</div>
<pre>
';
return (string)$monthList;
}
?></pre>
<div class="block block-month-list"></div>
<pre>
<!--?php /* END OF block-month-list */ ?-->
That’s it. Hope some of you find it useful.
Published in:
Leave a comment
2 comments
Hi Branko,
The code works as expected.
Thanks for sharing.
How To Display Product List Page >…..