Get Product Collection based on brand parameter from cms page
-
09-12-2020 - |
Domanda
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 ?
Soluzione
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.
In
.phtml
call a function$myProducts = $this->foo($my_var);
In
.php
, block file, write this function:public function foo($my_var) { $model = Mage::getModel('myExtension/myModel'); return $model->getProductCollectionByBrand($my_var); }
and now in model file:
public function getProductCollectionByBrand($my_var) { $collection = Mage::getModel('catalog/product')->getCollection(); $_productCollection = $collection->addAttributeToFilter('brand', $my_var); return $_productCollection; }
Altri suggerimenti
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());
}
}
?>