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?