让Magento按照年份/月份排列产品

2,125 人次阅读
没有评论

共计 3180 个字符,预计需要花费 8 分钟才能阅读完成。

让Magento按照年份/月份排列产品

做一个新模板,让Magento像画廊一样排列产品,由于有一个商店用得着这样的功能,所以就在想办法,让Magento能够排列出月份、年度信息。下面一段代码,可以实现理出月份,从1月到12月,显示出每个月的产品。你可以用这些来实现显示任何一个月的产品。只需将这些代码放入某些foreach 循环中。这个代码使你有一个Block来列出月份下包含的产品和链接。点击链接,会把你重定向到一个包含该月份下产品的页面。说话太绕口了,没有办法!

[php]
<!–?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 */ ?–>
[/php]

好了,希望你们能有一天会用上!

正文完
 0