2011-10-10 55 views
0

以下代码获取我所有的厂家在我所有的商店:获取所有厂商特定的商店

$attribute = Mage::getSingleton('eav/config')->addStoreFilter($storeId)->getAttribute('catalog_product', 'manufacturer'); 
if ($attribute->usesSource()) { 
    $options = $attribute->getSource()->getAllOptions(false); 
} 

但是我有多个门店,我只希望厂家为我选择的店铺ID。

我已经尝试添加商店过滤器,但没有运气。

任何人有任何想法如何通过存储过滤此?

回答

1
$product = Mage::getModel('catalog/product'); 
$storeId = Mage::app()->getStore()->getId();  

$attributes = Mage::getResourceModel('eav/entity_attribute_collection') 
    ->setStoreId($storeId); 
    ->setEntityTypeFilter($product->getResource()->getTypeId()) 
    ->addFieldToFilter('attribute_code', 'manufacturer') // This can be changed to any attribute code 
    ->load(false); 


$attribute = $attributes->getFirstItem()->setEntity($product->getResource()); 

/* @var $attribute Mage_Eav_Model_Entity_Attribute */ 
$manufacturers = $attribute->getSource()->getAllOptions(false); 
return $manufacturers; 

这应该这样做。您可以降低要选择的属性。

$product = Mage::getModel('catalog/product'); 
$products = $product->setStoreId($storeId)->getCollection() 
       ->addAttributeToSelect(array('name', 'price', 'small_image','short_description','manufacturer'), 'inner'); 

     $this->setProductCollection($products); 
+0

谢谢,但使我有以下错误:致命错误:调用未定义的方法Mage_Eav_Model_Mysql4_Entity_Attribute_Collection :: addStoreFilter()。有什么想法吗? – sulman

+0

我添加了setStoreId而不是addStoreFilter,请立即尝试。如果这不起作用,您将需要更改我们正在处理的集合。我会添加另一个选项。 –

+0

谢谢,但我无法让它工作。 TBH我认为这个处理时间可能太重,因为我会把它放在主页上。想想我最终会手动添加制造商。不过谢谢。 – sulman

0

我得到了制造商的名单,从目前的店内使用的产品数

public function getAllManu() 
{ 
    $product = Mage::getModel('catalog/product'); 
    $attributes = Mage::getResourceModel('eav/entity_attribute_collection') 
       ->setEntityTypeFilter($product->getResource()->getTypeId()) 
       ->addFieldToFilter('attribute_code', 'manufacturer'); //can be changed to any attribute 
    $attribute = $attributes->getFirstItem()->setEntity($product->getResource()); 
    $attrMenuItems = $attribute->getSource()->getAllOptions(false); 

    foreach ($attrMenuItems as $key => $value) { 
     $collection = Mage::getModel('catalog/product')->getCollection(); 
     $collection->addFieldToFilter(array(array('attribute' => 'manufacturer', 'eq' => $value['value']))); 
     $numberOfProducts = count($collection); 
     $attrMenuItems[$key]['products_count'] = $numberOfProducts; 
    } 

    return $attrMenuItems; 
}