Magento产品集合限制通过XML [英] Magento Product Collection Limit via XML

查看:129
本文介绍了Magento产品集合限制通过XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bestsellers模块,我写了,它的工作伟大,但我想能够改变通过XML返回的集合大小,而不是php / phtml。



像这样:

 < block type =catalog / product_listname = bestsellerslimit =3
template =custom / bestsellers.phtml/>

或类似:

 < block type =catalog / product_listname =bestsellers
template =custom / bestsellers.phtml>
< action method =setLimit> 3< / action>
< / block>这是可能吗?


b m当前正在通过phtml改变限制:

   - > setPageSize(3)
- > setCurPage 1);

但是这是硬编码和讨厌,我需要能够使用我的phtml文件作为模板



提前感谢任何人可以阐明这一点!

解决方案

Mage_Catalog_Block_Product_List 继承 Varien_Object 包含方法 getData() setData()的类以及魔术方法 get *() set *()。这些方法允许我们在对象中存储(你猜到)键控数据。



< action /> 标签在XML中允许我们对块实例执行方法调用。您的第二个示例就近了,但语法是:

 < block type =catalog / product_list =bestsellers> 
< action method =setLimit>< value> 3< / value>< / action&
< / block>

这相当于:

 < block type =catalog / product_listname =bestsellers> 
< action method =setData>< key> limit< / key>< value> 3< / value>< / action&
< / block>

这大致相当于:

  $ block = new Mage_Catalog_Block_Product_List(); 
$ block-> setLimit(3);

在对象中设置数据,我们现在可以通过 getData )$ this-> getLimit()通过调用 $ this-> getData('limit')制作我们的块代码:

   - > setPageSize($ this-> getLimit())
- > setCurPage(1);

您应该检查 limit 数据,如果XML中没有提供,则提供默认值。



注意< action /> 标记无关紧要。这是重要的参数的顺序。我们可以直接调用< action method =setLimit>< foo> 3< / foo>< / action>


I've got a bestsellers module which I've written and it works great, however I want to be able to change the collection size it returns via the XML, rather than the php/phtml.

Something like this:

    <block type="catalog/product_list" name="bestsellers" limit="3" 
template="custom/bestsellers.phtml" />

or something like:

    <block type="catalog/product_list" name="bestsellers" 
template="custom/bestsellers.phtml">
         <action method="setLimit">3</action>
    </block>

Is this possible?

I'm currently changing the limit via the phtml with:

->setPageSize(3)
->setCurPage(1);

But that is hard coded and nasty, I need to be able to use my phtml file as template for many cases of the bestsellers module being called from anywhere with the XML + limit in the XML.

Thanks in advance if anyone can shed light on this!

解决方案

The block Mage_Catalog_Block_Product_List inherits from the Varien_Object class which contains the methods getData() and setData(), as well as the magic methods get*() and set*(). These methods allow us to store (you guessed it) keyed-data within an object.

The <action /> tags in the XML allows us to perform method calls on the block instances. You're nearly there with your second example, but the syntax is:

<block type="catalog/product_list" name="bestsellers">
    <action method="setLimit"><value>3</value></action>
</block>

Which is equivalent to:

<block type="catalog/product_list" name="bestsellers">
    <action method="setData"><key>limit</key><value>3</value></action>
</block>

Which is roughly equivalent to:

$block = new Mage_Catalog_Block_Product_List();
$block->setLimit(3);

With the data set in the object we can now access through the getData() or get*() methods by calling $this->getLimit() or $this->getData('limit') making our block code:

->setPageSize($this->getLimit())
->setCurPage(1);

You should probably perform a check for the existence of the limit data first and provide a default value if none is provided in the XML.

Note: The name of the children in the <action /> tag don't matter. It's the order of the arguments that's important. We could just as well have called <action method="setLimit"><foo>3</foo></action> and it still would have worked.

这篇关于Magento产品集合限制通过XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆