开发者

Finding all products with the same attribute on Magento

I'm trying to do something that would seem simple but just doesn't work

I'm running Magento 1.5.1.

I would like to show on the product page all the items that have the same attribute set as the original item.

For example:

Item A has XYZ attributes, show me all the other items that have xyz attributes.

Nothing I've tried worked so far...

EDIT:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');      
$collection->addAttributeToSelect('orig_price');        

//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B
$collection->addFieldToFilter(array(
        array('name'=>'orig_price','eq'=>'Widget A'),
        array('name'=>'orig_price','eq'=>'Widget B'),           
));

foreach ($collection as $product) {
        //var_dump($product);
        var_dump($product->getData());
}

EDIT 2: I tried :

 $collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('color');

//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B 
$collection->addAttributeToFilter(array( array('name'=>'color','eq'=>'red'开发者_如何学C)
));

foreach ($collection as $product) { //var_dump($product); 
var_dump($product->getData()); }

and nothing at all showed up


First you should check that the attribute value for 'Used in Product Listing' is set to yes and than try the code below. I feel it will help you.

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('color', 'red');
foreach ($collection as $product) { //var_dump($product); 
var_dump($product->getData()); }

once you get the filtered data than add other select & filter attribute as per your requirement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜