Magento带分页添加新产物至CMS Page

在Magento我们常常有的做法是成立一个可以挪用新产物的block,然后通过:

{{block type="catalog/product_new" column_count="6" products_count="100" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml"}}

挪用,这样就可以在Magento首页显示新到产物 可能其他位置挪用,前概要有添加新产物并配置新产物期间了。

此刻是要让Magento建设一个带分页成果的新到产物展示Page,自动展示有配置了“Set Product as New from Date - Set Product as New to Date”的新到产物。

话入正题。Magento在CMS Page添加带分页成果的新到产物页面步调如下;

建设路径:app/code/local/Mage/Catalog/Block/Product
 
建设文件:app/code/local/Mage/Catalog/Block/Product/New.php

New.php将包围掉magento core里的New.php,所以假如本来New.php有在利用的话local的这个文件别的定名区分隔。
 
添加以下内容到New.php

<?php
//chen@sjolzy.cn
class Mage_Catalog_Block_Product_New extends Mage_Catalog_Block_Product_List
{
function get_prod_count()
{
//unset any saved limits
Mage::getSingleton('catalog/session')->unsLimitPage();
return (isset($_REQUEST['limit'])) ? intval($_REQUEST['limit']) : 12;
}// get_prod_count

function get_cur_page()
{
return (isset($_REQUEST['p'])) ? intval($_REQUEST['p']) : 1;
}// get_cur_page

/**
* Retrieve loaded category collection
*
* @return Mage_Eav_Model_Entity_Collection_Abstract
**/
protected function _getProductCollection()
{
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToSort('news_from_date', 'desc')
->setPageSize($this->get_prod_count())
->setCurPage($this->get_cur_page());

$this->setProductCollection($collection);

return $collection;
}// _getProductCollection
}// Mage_Catalog_Block_Product_New
?>

生存后,建设CMS里的新到产物列表Page。添加后,配置Design的内容为以下内容:

<reference name="content">
<block type="catalog/product_new" name="product_new" template="catalog/product/list.phtml">
<action method="setCategoryId"><category_id>10</category_id></action>
<action method="setColumnCount"><column_count>6</column_count></action>
<action method="setProductsCount"><count>0</count></action>
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager" />
<action method="setDefaultGridPerPage"><limit>12</limit></action>
<action method="addPagerLimit"><mode>grid</mode><limit>12</limit></action>
<action method="addPagerLimit"><mode>grid</mode><limit>24</limit></action>
<action method="addPagerLimit"><mode>grid</mode><limit>36</limit></action>
<action method="addPagerLimit"><mode>grid</mode><limit>48</limit></action>
<action method="addPagerLimit" translate="label"><mode>grid</mode><limit>all</limit><label>All</label></action>
</block>
<action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>6</count></action>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
</reference>

这样就可以生存查察结果了。以下扼要备记几点说明:

<block type="catalog/product_new" name="product_new" template="catalog/product/list.phtml">

配置块的范例和利用的模版文件,这个文件也是产物列表页用的。“catalog/product_new” 这个就是改换了的New.php。

<action method="setCategoryId"><category_id>10</category_id></action>

setCategoryId 配置目次ID,可以是网站的根目次ID,可能某目次。

<action method="setColumnCount"><column_count>6</column_count></action>

setColumnCount 新到产物的Page产物每页产物数为6

<action method="setProductsCount"><count>0</count></action>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/10669.html