Magento - creating a new getProductCollection () function

Currently, if I want to get a specific set of products, for example, the best-selling, I use the following directly in the 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 using the foreach statement.

Can someone explain how to make a new block for this that can be reused? I found several examples, but they always call the list of products from the CMS page, while I want the function call to be embedded directly in the template file, which I can call from anywhere.

So, suppose I have my module installed and my Bestseller.php file in the Block folder. In it, I assume that I set 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();
}

, ? - ?

$_productCollection = $this->getLoadedBestsellingCollection()

, , !

UPDATE:

, 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, . 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();

?

+3
1

HTML. phtml. $this phtml, .

, HTML. , /.

, , ,

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

-

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

Source: https://habr.com/ru/post/1767598/


All Articles