Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

addAttributeToFilter using attribute id instead of value
 
dan_burg1
Jr. Member
 
Total Posts:  3
Joined:  2013-04-19
 

I am trying to filter a collection based on a custom attribute I made called motorcycle_model, a multi select attribute (varchar).
However, addAttributeToFilter is fitlering based on the attributes ID instead of value.

For example, when i want to add a filter for “GSXR 750” I have to filter by 111, the attribute ID of GSXR 750.
How can I filter by value instead of the ID?
ex, instead of

$_products->addAttributeToFilter('motorcycle_model'"111");

Using this

$_products->addAttributeToFilter('motorcycle_model'"GSXR 750");

The values are being stored correctly, because calling

$product->getResource()->getAttribute('motorcycle_model')->getFrontend()->getValue($product)
returns getting the actual value of the atrribute, in this case “GSXR 750” instead of the values ID, 111

My full code is here

<?php
//needed to extend mage app
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

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

//works, but working by relative key from model name. 111 = gsxr 750
$allowedModels = array("111","42");
//$allowedModels = array("GSXR 750","GSXR 600","GSXR600");

$condArr = array();
foreach(
$allowedModels as $model)
{
  $condArr[] 
= array($model);
  
$condArr[] = array('like' =>$model',%');
  
$condArr[] = array('like' => '%,' $model);
  
$condArr[] = array('like' => '%,' $model ',%');
}
$_products
->addAttributeToFilter('motorcycle_model'$condArr);
$_products->load();


$resultsArr $_products->getItems();


if (
$_products->getFirstItem()) 
{
    
foreach($resultsArr as $r)
    
{    
        
echo "<br>" $r->getName() . " " ."<br>";
        echo 
"<br>" $r->getResource()->getAttribute('motorcycle_model')->getFrontend()->getValue($r) . "<br>";
        
//echo "<br>" . $r->getData();
    
}
}
else
{
    
echo "<br> No product exists with the filter <br> ";
}


?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
Anshu Mishra
Sr. Member
 
Total Posts:  101
Joined:  2012-04-23
India
 

Hi

You can go through the below link to get good knowledge of using addAttributeToFilter
http://fishpig.co.uk/blog/addattributetofilter-conditionals-in-magento.html

 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_burg1
Jr. Member
 
Total Posts:  3
Joined:  2013-04-19
 

Thanks for the response, this has been driving me crazy.  However, it did not help me fix my issue.  I appear to be calling addAttributeToFilter correctly, and nothing on that page addresses returning ID instead of value.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Anshu Mishra
Sr. Member
 
Total Posts:  101
Joined:  2012-04-23
India
 

Have you tried below line of code?

$_products->addAttributeToFilter('motorcycle_model', array('eq' => 'GSXR 750'));
I guess it would work.
 
Magento Community Magento Community
Magento Community
Magento Community
 
kiatng
Enthusiast
 
Total Posts:  875
Joined:  2008-09-03
Kuala Lumpur, Malaysia
 

One approach is to get the collection of options for the attribute ‘motorcycle_model’, then filter that collection with your $condArr. Finally get the array of option_id and use that for the product filtering

Here’s how to get collection of options for the attribute:

$entityType Mage::getSingleton('eav/config')->getEntityType(Mage_Catalog_Model_Product::ENTITY);
$attribute Mage::getModel('eav/entity_attribute')->loadByCode($entityType'motorcycle_model'); 
$collection Mage::getResourceModel('eav/entity_attribute_option_collection')
    ->
setAttributeFilter($attribute->getId())
    ->
setStoreFilter($attribute->getStoreId())
    ->
addFieldToFilter('value'$condArr());

Now the product filtering:

$options $collection->toOptionArray();
$_products->addAttributeToFilter('motorcycle_model', array('in'array_keys($options)));

The above are totally untested and may contain all kinds of errors.

 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_burg1
Jr. Member
 
Total Posts:  3
Joined:  2013-04-19
 

Thanks for all the responses.

@Anshu_Mishra That only retuerns the attribute ID

@kiatng I will defiantly try that out

I Ended up doing the following which works, not sure about performance though.

function getIdFromValue(array $value)
{
    $results 
= array();
    
$productModel Mage::getModel(\'catalog/product\');
    
$attr $productModel->getResource()->getAttribute(\"motorcycle_model\");

    foreach(
$value as $v)
    
{
        
if ($attr->usesSource())
        
{
            $x 
$attr->getSource()->getOptionId($v); 
            if(
$x != null)
            
{
                $results[] 
$x;
            
}
        }  
    }
    
echo \"<br>\";
    
var_dump($results);
    return 
$results;
}
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top