Magento - 创建一个新的 getProductCollection() 函数

发布于 2024-09-25 10:35:50 字数 2217 浏览 2 评论 0原文

目前,如果我想获取某个产品集合,例如畅销产品,我会直接在模板文件中使用以下内容:

$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('name')
->addAttributeToFilter('visibility', $visibility)
->addOrderedQty()
->setOrder('ordered_qty', 'desc')
$_productCollection->load();

...然后使用 foreach 语句提取产品。

谁能解释如何制作一个可以重复使用的新块来执行此操作?我找到了一些示例,但它们总是从 CMS 页面调用产品列表,而我希望调用直接嵌入模板文件中的函数,这样我就可以从任何地方调用该函数。

所以假设我已经设置了模块,并且我的 Bestseller.php 文件位于我的 Block 文件夹中。我想我在其中放置了集合的函数,例如

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_Catalog_Model_Resource_Eav_Mysql4_Product_Collection类时遇到了麻烦。如果我将代码添加到 Collection.php 文件的末尾,例如

public function addBestSelling()
{
$this->addAttributeToSelect('*')->addOrderedQty()->setOrder('ordered_qty', 'desc'); 
return $this;
}

然后

$_productCollection = Mage::getResourceModel('reports/product_collection')->addBestSelling();

在我的 phtml 模板文件中使用,它就可以正常工作。但是,如果我将该代码分离到我的模块的 Models 文件夹中的 Bestseller.php 中,就像这样,

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;
}
}

然后尝试将其与以下内容一起使用,则会出现页面未完成的错误正在加载(没有错误消息)

$_productCollection = Mage::getResourceModel('featuredproducts/bestseller')
->addMostViewed();

我缺少什么?

Currently, if I want to get a certain collection of products, for example best-selling, I use the following, direct in my template file:

$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('name')
->addAttributeToFilter('visibility', $visibility)
->addOrderedQty()
->setOrder('ordered_qty', 'desc')
$_productCollection->load();

...and then pull out the products with a foreach statement.

Can anyone explain how to make a new block to do this, that can be re-used? I've found a few examples but they always call the product list from a CMS page, whereas I want to have the call to the function embedded directly in a template file, that I can call from anywhere.

So presume I have my module set up, and my Bestseller.php file in my Block folder. In it I guess I put my function for the collection, something like

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();
}

And if so, how then do I call that from my template? Something like?

$_productCollection = $this->getLoadedBestsellingCollection()

Any help, or pointers to decent tutorials, much appreciated!

UPDATE:

I'm getting closer, but I'm having trouble with extending the Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection class. If I add my code to the end of the Collection.php file, like

public function addBestSelling()
{
$this->addAttributeToSelect('*')->addOrderedQty()->setOrder('ordered_qty', 'desc'); 
return $this;
}

and then use

$_productCollection = Mage::getResourceModel('reports/product_collection')->addBestSelling();

in my phtml template file, it works fine. But if I separate that code into my Bestseller.php, in my Models folder of my module, like so

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;
}
}

and then try and use it with the following, I get an error where the page doesn't finish loading (no error message)

$_productCollection = Mage::getResourceModel('featuredproducts/bestseller')
->addMostViewed();

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○愚か者の日 2024-10-02 10:35:50

块用于渲染 HTML。每个块对象都有一个 phtml 模板对象。当您在 phtml 模板中使用 $this 时,您将引用回包含块对象。

听起来不像是在渲染 HTML。听起来您想获取特定的产品列表以在任何块/模板中使用

如果上述假设正确,您不需要创建一个新的块,而是创建一个新的模型集合类,该类扩展通过

Mage::getResourceModel('reports/product_collection').  

将方法添加到该类返回的对象的类,并使用类似的内容调用它

Mage::getResourceModel('mymodule/my_collectionclass')-> getLoadedBestsellingCollection()

Blocks are for rendering HTML. Each Block Object has a phtml template object. When you use $this from a phtml template you're referring back to the containing block object.

It doesn't sounds like you're rendering HTML. It sounds like you want to fetch a specific list of products to use in any block/template.

If the above assumptions are correct, instead of creating a new Block you want to create a new Model Collection class that extends the class of the object returned by

Mage::getResourceModel('reports/product_collection').  

Add your method to that class, and call it with something like

Mage::getResourceModel('mymodule/my_collectionclass')-> getLoadedBestsellingCollection()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文