简体   繁体   中英

Magento -> How can I list products with same multiple select attribute?

I'm trying to retrieve products that carry same attribute. Specifically multiple select type. It seems the basic methods don't work. Selecting only the "name" attribute, I get all my products listed. When I try to filter "shop_by_color", it filters down, but not entirely. Not sure why it removes some and leaves others, even though they are the wrong ones. Any tips appreciated.

<?php 

$model = Mage::getModel('catalog/product');

$collection = $model->getCollection();
$collection->addAttributeToSelect('name');
$collection->addAttributeToFilter('shop_by_color'); // multiple select attribute
$collection->addFieldToFilter(array(array('attribute'=>'shop_by_color','finset'=>array('Yellow, White'),
)));

$collection->load();

?>

<ul>
<?php foreach($collection as $product) : ?>
<li><a href="<?php echo $product->getProductUrl() ?>"><?php echo $product->getName() ?></a></li>            
<?php endforeach; ?> 
</ul>

Hi I am not sure about your syntax I have never seen this sort of thing before.

<ul>
<?php foreach($collection as $product) : ?>
<li><a href="<?php echo $product->getProductUrl() ?>"><?php echo $product->getName()
 ?></a></li>            
<?php endforeach; ?> 
</ul>

Shouldn't it be...

<ul>
<?php foreach($collection as $product) { ?>
<li><a href="<?php echo $product->getProductUrl() ?>"><?php echo $product->getName()
 ?></a></li>            
<?php } ?> 
</ul>

DC

<?php 
 $_productCollection=$this->getLoadedProductCollection();
 $_productCollection->clear()
->addAttributeToSelect('*')
->addAttributeToFilter('type_id', array('eq' => 'simple'))
->addAttributeToSort('name', 'ASC')
->addAttributeToSort('created_at', 'ASC')
->load();?>

on list page please use this type collection filter it's working fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM