你现在的位置:首页 > PHP网站建设知识库 > magento > 正文

让magento显示特殊价格日期的商品

magento后台添加商品时,可以设置商品的价格和日期,现在我们想在magento前台的页面显示设置有特殊价格日期的产品,magento本身并未提供这个功能,我们自己来做magento网站开发

\

1) 新建一个Page页,Design的内容为

<reference name="content">
   <block type="catalog/product_special " name="product_special " template="catalog/product/list.phtml">
      <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>

2) 建立文件:app/code/local/Mage/Catalog/Block/Product/Special.php,内容为:

<?php

class Mage_Catalog_Block_Product_Special extends Mage_Catalog_Block_Product_List{
   function get_prod_count(){
      Mage::getSingleton('catalog/session')->unsLimitPage();
      return (isset($_REQUEST['limit'])) ? intval($_REQUEST['limit']) : 12;
   }

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

   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('special_from_date ', array('date' => true, 'to' => $todayDate))
         ->addAttributeToFilter('special_to_date ', array('or'=> array(
            0 => array('date' => true, 'from' => $todayDate),
            1 => array('is' => new Zend_Db_Expr('null')))
         ), 'left')
         ->addAttributeToSort('special_from_date', 'desc')
         ->setPageSize($this->get_prod_count())
         ->setCurPage($this->get_cur_page());
      $this->setProductCollection($collection);
      return $collection;
   }
}
?>

3) 访问Page的url,已经看见设置有特殊日期和价格的产品了。