开发者

Magento - making a new getProductCollection() function

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')
->addAttributeToFilte开发者_JAVA技巧r('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?


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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜