首页 > 代码库 > magento首页调用最新产品

magento首页调用最新产品

这个需要我们自己添加一个block块供我们调用,可参考new products的block类,建立文件app/code/core/Mage/Catalog/Block/Product/Special.php

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    <?php  
    class Mage_Catalog_Block_Product_Special extends Mage_Catalog_Block_Product_Abstract  
    {  
        protected $_productsCount= null;  
        const DEFAULT_PRODUCTS_COUNT = 5;
 
        protected function _beforeToHtml()  
        {  
            $todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);  
            $collection = Mage::getResourceModel(‘catalog/product_collection‘);  
            Mage::getSingleton(‘catalog/product_status‘)->addVisibleFilterToCollection($collection);  
            Mage::getSingleton(‘catalog/product_visibility‘)->addVisibleInCatalogFilterToCollection($collection);  
            $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->getProductsCount())  
                        ->setCurPage(1);  
            $this->setProductCollection($collection);  
            return parent::_beforeToHtml();  
        }  
        public function setProductsCount($count)  
        {  
            $this->_productsCount = $count;  
            return $this;  
        }  
        public function getProductsCount()  
        {  
            if(null === $this->_productsCount)  
            {  
                $this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;  
            }  
            return $this->_productsCount;  
        }  
    }

复制app/design/frontend/base/default/template/catalog/product/new.phtml到app/design/frontend/theme/default/template/catalog/product/special.phtml,修改为相应标题和产品显示数量
在后台首页的CMS Page模块添加调用代码:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
<block type="catalog/product_special" name="home.catalog.product.special" alias="product_special" template="catalog/product/special.phtml" after="cms_page">
    <action method="addPriceBlockType">
        <type>bundle</type>
        <block>bundle/catalog_product_price</block>
        <template>bundle/catalog/product/price.phtml</template>
    </action>
    <action method="setColumnCount">
        <count>4</count>
    </action>
    <action method="setProductsCount">
        <count>4</count>
    </action>
        </block>

当然要想显示特价产品,在产品管理处还是要设置的,进入产品管理的Prices栏,Special Price和Special Price From Date都要设置,那么前台就会显示特价产品。

magento首页调用最新产品