1

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 ?

2

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;
    }
    
  • Hi thanks, yes that's a custom attribute, name may vary later as I'd have to repeat for some other attributes too, just wanted to know the approach. I was aware of getData() however not sure how would that be available to model file – echoashu Jan 25 '16 at 2:26
  • Yes I'm calling product collection in model file in its custom block folder – echoashu Jan 25 '16 at 2:26
  • Remember, the $my_var is only accessible from .phtml and block files, not in model. – Adarsh Khatri Jan 25 '16 at 2:27
  • Yes that's the point So that said I'll have to modify my model function to accept a parameter and pass that from block file correct ? $products = Mage::getModel("brandproducts/brandproducts")->getBrandProducts($my_var); in // app/code/local/Mycomp/Brandproducts/Block/Brandproducts.php – echoashu Jan 25 '16 at 2:29
  • 1
    oh! you already got it. :) BTW please check my updated answer too. – Adarsh Khatri Jan 25 '16 at 2:33
2

Add bellow code in your file

app/design/frontend/your_package/your_theme/template/catalog/brand/brandprod_listing.phtml

If your product attribute brand input type text then add bellow code.

<?php $brand = $this->getBrandName();?>
<?php $_productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToSelect('brand')
                ->addAttributeToFilter('brand',array('eq' => $brand));
?>
<?php 
    if(count($_productCollection)) {
        foreach($_productCollection as $product) {
            echo ($product->getName());
        }
    }
?>

If your product attribute brand input type select then add bellow code.

<?php $brand = $this->getBrandName();?>         
<?php
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'brand');
$optionId = '';
if ($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
    foreach($options as $option) {
        if (trim(strtolower($option['label'])) == trim(strtolower($brand))) {
            $optionId    = $option['value'];
            break;
        }
    }
}
?>

<?php $_productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToSelect('brand')
                ->addAttributeToFilter('brand',array('eq' => $optionId));
?>
<?php 
    if(count($_productCollection)) {
        foreach($_productCollection as $product) {
            echo ($product->getName());
        }
    }
?>
  • Thanks Abdul, I was going with the same approach, however then i wanted to call collection in model file ( creating a custom block) so was not sure how to access $this->getData() in model file – echoashu Jan 25 '16 at 17:12
  • when the .phtml file is not the part of custom block but just a simple file in template folder /template/catalog/brand/brand_prod_listing.phtml i cannot retrive variable data by $this->getdata('brand_name') or $this->getBrandName() – echoashu Jan 28 '16 at 1:47
  • have you added brand_name in cms page??? – Abdul Jan 28 '16 at 4:31
  • yes i did for sure :) however it does not work with the phtml file only and if i create a complete custom block and call the corresponding phtml file from backend then only getdata or getParaName works – echoashu Jan 28 '16 at 6:59
  • can u share your complete code in qus?? – Abdul Jan 28 '16 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.