Magento新闻插件的另一种用法:

Magento 新闻插件KEY: https://connect20.magentocommerce.com/community/MWD_News_and_Testimonials_with_Images_Extension

Magento有些客户会要求将最新的新闻放到首页进行浏览. 而这个MWD News插件只是在内面进行浏览.

1.先进入后台: System->Magento Connect-> Magento Connect Manager.输入用户,密码进入后,将https://connect20.magentocommerce.com/community/MWD_News_and_Testimonials_with_Images_Extension KEY给安装好.
2.在Layout(布局)文件config.xml中插件以下代码,说到Layout, Magento的执行控制器不直接将数据传给试图,相反的视图将直接引用模型,从模型取数据。这样的设计就导致了视图被拆分成两部分,块(Block)和模板(Template)。如下:

在这里,我是将文件放到首页的左边,所以用LEFT(看个人要求如:right,top等等).

3.在template文件夹里创建profile/homenews.phtml, 每一个块都和一个唯一的模板文件绑定。在模板文件phtml中,“$this”就是指该模板文件对应的快对象。写入以下代码:

What’s Hot!

 

 

 

/dt>

 

 

 

See More…

 

这些代码是只要首页显示,里面的DIV什么的都自己改了下…在Magento代码中,其实每个Model都有个Collection。了解这些数据收集器是如何工作的是你成为一个真正Magento开发人员的关键点。Magento中所有的Model都继承Varien_Object,在面向对象编程中,这样做的好处是当你想往多个Model中添加方法的时候,你只需要简单地修改一个文件即可。

这里要讲一下的是$news = $this->getHomenews(3);这个函数是自己定义的.里面的参数3是显示的新闻条目数.

在appcodecommunityRichardMasonProfileBlockProfile.php中加入以下代码:

public function getHomenews($int){

return $this->getProfileshome(RichardMason_Profile_Model_Profile::CATEGORY_NEWS,$int);

}

public function getProfileshome($category_id,$int) {

$profiles = Mage::getModel(‘profile/profile’)->getCollection()

->addStoreFilterhome(Mage::app()->getStore()->getId(),$int);

$profiles->addFieldToFilter(‘category_id’, $category_id);

$profiles->setOrder(“creation_time”, “DESC”);

return $profiles;

}

这两个函数.

再在appcodecommunityRichardMasonProfileModelMysql4ProfileCollection.php文件里加入以下代码,这里的limit($int)就是在查询过程之中加入的一个条件,即上面$news = $this->getHomenews(3);这句代码里传过来的参数,这里我为了方面就改了出来:

public function addStoreFilterhome($store,$int, $withAdmin = true)

{

if (!$this->getFlag(‘store_filter_added’)) {

if ($store instanceof Mage_Core_Model_Store) {

$store = array($store->getId());

}

$this->getSelect()->join(

array(‘store_table’ => $this->getTable(‘profile/profile_store’)),

‘main_table.profile_id = store_table.profile_id’,

array()

)

->where(‘store_table.store_id in (?)’, ($withAdmin ? array(0, $store) : $store))

->group(‘main_table.profile_id’)->limit($int);

$this->setFlag(‘store_filter_added’, true);

}

return $this;

}

用于magento数据库的操作, 它充分利用了Varien Data Collections常用Array来做数据收集器这一点,在使用Varien_Data_Collection来做数据收集的时候,它实现了php内置IteratorAggregate对象迭代器和Countable两个接口。 这就完成了操作,希望可以帮助到你.