tags:

views:

95

answers:

1

Hi All

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?

+1  A: 

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()
Alan Storm
Thanks Alan. Read some of your posts on your site and it's getting clearer! So, say I have Bestseller.php in my modules Model folder - the class is **Samsmodule_FeaturedProducts_Model_Bestseller** - do I just add my same call for the **_getBestsellingCollection** as in my question? And then call it in my template using **$_productCollection = Mage::getResourceModel('featuredproducts/bestseller')->getLoadedBestsellingCollection()**? I tried this and it's not working, so I think I'm still missing something! I do have my models set up correctly as I'm adding stuff to the admin with this module.
Sam
You're creating a new class. There's nothing special about Samsmodule_FeaturedProducts_Model_Bestseller. It's just the a default model. You're going to define a **new** class which extends the reports/product_collection. Then you can add methods to your hearts content.
Alan Storm
See update to my question
Sam
Roll up your sleeves and do some good old fashioned debugging. My guess is that getResourceModel isn't returning an object because your URL/Class has the wrong naming convention. Take a look at _getResourceModelFactoryClassName to see what the system wants. (also, Check the magento system log as well as the PHP error logs)
Alan Storm
See the new thread for a more in-depth question/answer: http://stackoverflow.com/questions/3877263/magento-extending-the-mage-catalog-model-resource-eav-mysql4-product-collection
Sam