Magento 1.9 Product Collection – Get Product Collection Based on Brand Parameter from CMS Page

custom blockmagento-1.9modelproduct-collection

I'm creating custom block to get products based on the brand name

I'd like to pass the brand name parameter from the cms page like below

{{block type="core/template" template="catalog/brand/brandprod_listing.phtml"  brand_name="mybrandname2"}}

However not sure how would i load my product collection in Block Model file dynamicallay receiving the brand_name parameter

Here is my model.

$collection = Mage::getModel('catalog/product')->getCollection();
$_productCollection = $collection->addAttributeToFilter('brand','mybrandname');  

However as can be seen, how to get the parameter passed from cms page and load the results ?

Best Answer

This should help you:

{{block ... my_var="value here" ... template="catalog/brand/brandprod_listing.phtml"}}

If you want to get the my_var in php block directly in php block file then try this:

$my_var = $this->getMyVar();

Did you notice **capital M and V? This is magento default magic method.

Or you simply want to display $my_var value in your phtml file catalog/brand/brandprod_listing.phtml

$my_var = $this->getData('my_var');

For example:

$collection = Mage::getModel('catalog/product')->getCollection();
$_productCollection = $collection->addAttributeToFilter('brand', $my_var); 

Note

Is attribute name is really brand? I believe it should be manufacturer for brands.

[UPDATE 1]

How are you calling product collection? Are you calling in .phtml file? If you are, I would suggest to call it in block files.

[UPDATE 2]

To have this $my_var in your model you need to call it from block.

  1. In .phtml call a function

    $myProducts = $this->foo($my_var);
    
  2. In .php, block file, write this function:

    public function foo($my_var)
    {
       $model = Mage::getModel('myExtension/myModel');
       return $model->getProductCollectionByBrand($my_var);
    }
    
  3. and now in model file:

    public function getProductCollectionByBrand($my_var)
    {
       $collection = Mage::getModel('catalog/product')->getCollection();
       $_productCollection = $collection->addAttributeToFilter('brand', $my_var);
       return $_productCollection;
    }