Magento-创建新的getProductCollection()函数
目前,如果我想获得某一系列产品,例如畅销产品,我会直接在模板文件中使用以下内容:Magento-创建新的getProductCollection()函数,magento,Magento,目前,如果我想获得某一系列产品,例如畅销产品,我会直接在模板文件中使用以下内容: $_productCollection = Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('name') ->addAttributeToFilter('visibility', $visibility) ->addOrderedQty() ->setOrder('ordered_qty
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('name')
->addAttributeToFilter('visibility', $visibility)
->addOrderedQty()
->setOrder('ordered_qty', 'desc')
$_productCollection->load();
…然后用foreach语句取出产品
谁能解释一下如何制作一个可以重复使用的新块?我发现了一些示例,但它们总是从CMS页面调用产品列表,而我希望将对函数的调用直接嵌入到模板文件中,我可以从任何地方调用
假设我已经设置好了模块,并且我的Block文件夹中有Bestseller.php文件。我想我把我的收集功能放进去了,比如
protected function _getBestsellingCollection()
{
$_BestsellingCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('name')
->addAttributeToFilter('visibility', $visibility)
->addOrderedQty()
->setOrder('ordered_qty', 'desc');
$_BestsellingCollection->load();
}
public function getLoadedBestsellingCollection()
{
return $this->_getBestsellingCollection();
}
如果是这样,那么我如何从我的模板中调用它?什么样的
$_productCollection = $this->getLoadedBestsellingCollection()
非常感谢您的任何帮助,或提供体面教程的指导
更新:
我越来越接近了,但是我在扩展Mage\u Catalog\u Model\u Resource\u Eav\u Mysql4\u Product\u Collection类时遇到了问题。如果我将代码添加到Collection.php文件的末尾,如
public function addBestSelling()
{
$this->addAttributeToSelect('*')->addOrderedQty()->setOrder('ordered_qty', 'desc');
return $this;
}
然后使用
$_productCollection = Mage::getResourceModel('reports/product_collection')->addBestSelling();
在我的phtml模板文件中,它运行良好。但是如果我将代码分离到我的畅销书.php,在我的模块的Models文件夹中,就像这样
class Samsmodule_FeaturedProducts_Model_Bestseller extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
{
public function addBestSelling()
{
$this->addAttributeToSelect('*')->addOrderedQty()->setOrder('ordered_qty', 'desc');
return $this;
}
}
然后尝试将其用于以下内容,我得到一个错误,页面没有完成加载(没有错误消息)
我缺少什么?块用于呈现HTML。每个块对象都有一个phtml模板对象。从phtml模板使用
$this
时,您指的是包含的块对象
听起来你不是在渲染HTML。听起来您想在任何块/模板中获取要使用的特定产品列表
如果上述假设正确,则不创建新块,而是要创建一个新的模型集合类,该类扩展
Mage::getResourceModel('reports/product_collection').
将您的方法添加到该类中,并使用以下内容调用它
Mage::getResourceModel('mymodule/my_collectionclass')-> getLoadedBestsellingCollection()
+谢谢你的提问,有很多例子。谢谢艾伦。在你的网站上读一些你的帖子,它会变得更清晰!那么,假设我的modules Model文件夹中有Bestseller.php,这个类是Samsmodule\u FeaturedProducts\u Model\u Bestseller,我是否要添加与我的问题相同的对_getbestselling集合的调用?然后在我的模板中使用$\u productCollection=Mage::getResourceModel('featuredproducts/bestseller')->getLoadedBestsellingCollection()调用它?我试过了,但它不起作用,所以我想我还是错过了一些东西!我已经正确设置了我的模型,因为我正在使用这个模块向管理员添加内容。您正在创建一个新类。Samsmodule\u FeaturedProducts\u Model\u畅销书没有什么特别之处。这只是一个默认模型。您将定义一个扩展reports/product\u集合的新类。然后你可以把方法添加到你心中的内容中。卷起袖子,做一些老式的调试。我猜getResourceModel没有返回对象,因为您的URL/类具有错误的命名约定。请查看_getResourceModelFactoryClassName以了解系统需要什么。(另外,请查看magento系统日志以及PHP错误日志)有关更深入的问题/答案,请参阅新线程:
Mage::getResourceModel('mymodule/my_collectionclass')-> getLoadedBestsellingCollection()