4

I need to retrieve all values of an attribute (multi-option select type) from a filtered collection of products.

First i want to get a collection of products, then filter this collection based on certain category and certain attribute.For remaining products in the collection, I want to retrieve the values (unique) of my multi-option select attribute.

So far I have some code that doesn't work

$attributeCode = 'compatible_printers';
$ctname=$_GET['ctn'];

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

$collection->addAttributeToSelect('name'); 
$collection->addAttributeToSelect('manufacturer');
$collection->addAttributeToSelect('ink_type');
$collection->addAttributeToSelect($attributeCode); // this is the required attribute

$collection->addFieldToFilter(array(
array('attribute'=>'manufacturer','eq'=>$ctname), 
 )); 


$collection->addFieldToFilter(array(
array('attribute'=>'ink_type','eq'=>'Laserjet Toner'), 
 )); 
$collection->addAttributeToFilter($attributeCode, array('notnull' => true));
    $collection->addAttributeToFilter($attributeCode, array('neq' => ''));

// i m trying to get the attribute values in the following array
$usedAttributeValues = array_unique($collection->getColumnValues($attributeCode));

I have looked for similar questions and found some anwsers but they don't work for me. Can someone advice how to make the above code work?

2

You'll need to get a collection of the desired products and iterate through it to get the attribute values. Like this..

$attributeCode = 'compatible_printers';
$ctname = $_GET['ctn'];
$usedAttributeValues = array();

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect(array('name', 'manufacturer', 'ink_type', $attributeCode))
    ->addAttributeToFilter('manufacturer', $ctname)
    ->addAttributeToFilter('ink_type', 'Laserjet Toner')
    ->addAttributeToFilter($attributeCode, array('notnull' => true))
    ->addAttributeToFilter($attributeCode, array('neq' => ''));

foreach ($collection as $product) {
    $usedAttributeValues[] = $product->getData($attributeCode);
}

$usedAttributeValues = array_unique($usedAttributeValues);
| improve this answer | |
0

Try

$attributeCode = 'compatible_printers';

$usedAttributeValues = array();
foreach($collection as $product){
   // get unique code
   $usedAttributeValues[$product->getData($attributeCode)] = $product->getAttributeText($attributeCode) 
}
| improve this answer | |
  • still not working. Shows blank with not error. – manu Feb 18 '15 at 23:49
0

Please try the below snippet, i think this could work

$attributeCode = 'compatible_printers';
$ctname = $_GET['ctn'];
$usedAttributeValues = array();

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')  //selected all attributes, if you want you can name it
    ->addAttributeToFilter('manufacturer', $ctname)
    ->addAttributeToFilter('ink_type', 'Laserjet Toner');    

foreach ($collection as $product) {
    print $product->getId(); //prints product id
    print $product->getSku(); //prints productsku id

    //Next comes your attribute options related to this product
    echo $product->getResource()->getAttribute($attributeCode)->getFrontend()->getValue($product); //It will be comma seprated values


}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.