2015-01-15 71 views
2

我想要在自定义模块的前端获取属于选定制造商的产品列表。我成功地获得了制造商名单,但我无法检索与特定制造商相关的产品。我搜查了很多,但我没有得到任何解决方案。如何获得制造商的产品?

这里是我的代码 块文件: -

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; 
} 
?> 

回答

0

正如上面提到,您需要遵循以下几个步骤:

1 )转到属性集,并确保将“制造商”分配给您正在使用的属性集。

2)确保您已将某些制造商添加到属性选项中。

3)将其中一个选项分配给您的产品。

根据您的Magento版本,这应该工作:

<?php echo $_product->getAttributeText('manufacturer') ?> 

可以看我的错误给你:

给出错误调用一个成员函数getManufacturer()一个非对象在

你确定你是这行之后把这个代码:

<?php $_product = $this->getProduct(); ?> 

希望这个解决方案让你一天.... !!!