Show Magento products by month and year

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>
&nbsp;
<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.

You made it all the way down here so you must have enjoyed this post! You may also like:

Shell script for converting configurable to grouped products Tsvetan Stoychev
Tsvetan Stoychev, | 5

Shell script for converting configurable to grouped products

Large number of input variables in Magento Kresimir Banovic
, | 5

Large number of input variables in Magento

Programmatically create bundle products in Magento Petar Sambolek
Petar Sambolek, | 5

Programmatically create bundle products in Magento

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.