如何通过制造商获取产品?
How to get products by manufacturer?
我想在自定义模块的前端获取属于所选制造商的产品列表。我成功获取了制造商列表,但我无法检索与特定制造商相关的产品。我搜索了很多,但我没有得到任何解决方案。
这是我的代码
块文件:-
public function __construct()
{
parent::__construct();
$brand_id = $this->getRequest()->getParam('id');
$layer = $this->getLayer();
//$collection = Mage::getModel('catalog/product')->getCollection()
$collection = $layer->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('manufacturer', 20);
$this->setCollection($collection);
}
public function getProductCollection()
{
if (is_null($this->_productCollection))
{
$layer = $this->getLayer();
//$collection = Mage::getModel('catalog/product')->getCollection()
$collection = $layer->getProductCollection()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addAttributeToFilter('manufacturer', 20)
->addStoreFilter(Mage::app()->getStore()->getId())
->addMinimalPrice()
->addUrlRewrite();
$this->_productCollection = $collection;
Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_productCollection);
}
return $this->_productCollection;
}
public function getColumnCount()
{
return 3;
}
这是我的 phtml 代码
<?php
$product = $this->getProductCollection();
foreach ($product as $prods)
{
echo '<pre>'; print_r($prods->getData()); die;
}
?>
如上所述,您需要执行以下几个步骤:
1) 转到属性集,并确保将 "manufacturer" 分配给您正在使用的属性集。
2) 确保您在属性选项中添加了一些制造商。
3) 为您的产品指定一个选项。
根据您的 magento 版本,这应该有效:
<?php echo $_product->getAttributeText('manufacturer') ?>
我可以看到您遇到的错误:
在
中对非对象的成员函数 getManufacturer() 调用时出错
您确定要将此代码放在此行之后吗:
<?php $_product = $this->getProduct(); ?>
希望这个解决方案让你开心....!!!
我想在自定义模块的前端获取属于所选制造商的产品列表。我成功获取了制造商列表,但我无法检索与特定制造商相关的产品。我搜索了很多,但我没有得到任何解决方案。
这是我的代码 块文件:-
public function __construct()
{
parent::__construct();
$brand_id = $this->getRequest()->getParam('id');
$layer = $this->getLayer();
//$collection = Mage::getModel('catalog/product')->getCollection()
$collection = $layer->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('manufacturer', 20);
$this->setCollection($collection);
}
public function getProductCollection()
{
if (is_null($this->_productCollection))
{
$layer = $this->getLayer();
//$collection = Mage::getModel('catalog/product')->getCollection()
$collection = $layer->getProductCollection()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addAttributeToFilter('manufacturer', 20)
->addStoreFilter(Mage::app()->getStore()->getId())
->addMinimalPrice()
->addUrlRewrite();
$this->_productCollection = $collection;
Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_productCollection);
}
return $this->_productCollection;
}
public function getColumnCount()
{
return 3;
}
这是我的 phtml 代码
<?php
$product = $this->getProductCollection();
foreach ($product as $prods)
{
echo '<pre>'; print_r($prods->getData()); die;
}
?>
如上所述,您需要执行以下几个步骤:
1) 转到属性集,并确保将 "manufacturer" 分配给您正在使用的属性集。
2) 确保您在属性选项中添加了一些制造商。
3) 为您的产品指定一个选项。
根据您的 magento 版本,这应该有效:
<?php echo $_product->getAttributeText('manufacturer') ?>
我可以看到您遇到的错误:
在
中对非对象的成员函数 getManufacturer() 调用时出错您确定要将此代码放在此行之后吗:
<?php $_product = $this->getProduct(); ?>
希望这个解决方案让你开心....!!!