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

set value of attribute of type select
 
jakilcz
Jr. Member
 
Total Posts:  14
Joined:  2008-09-01
 

Hi
I wonder how can i change value of attribute that is set as select.

Now i do it in a way that i think is extremely lame but works, anyway i’m sure there must be far easier way to do it, but i didn’t find it…

I hava attribute “seller_confirmation” and i don’t know how to get to its options easily!

I know i could use setSeller_confirmation(285). But what if i don’t know this code and want to set it to value “rejected” and this text value is all i know?

$attrs=$product->getAttributes();
        
        foreach(
$attrs as $attr{
            
if ($attr->getAttributeCode()=='seller_confirmation'{
                $attribute
=$attr;
                break;            
            
}
        }

        
        $collection 
Mage::getResourceModel('eav/entity_attribute_option_collection')
        ->
setPositionOrder('asc')
        ->
setAttributeFilter($attribute->getId())
        ->
setStoreFilter($attribute->getStoreId())
        ->
load();

        
        
$options_array=array();
        
        foreach(
$collection as $option{
            $options_array[$option
->getDefaultValue()]=$option->getOptionId();
        
}
        
        $product
->setSeller_confirmation($options_array['rejected']);
        
$product->save();
 
Magento Community Magento Community
Magento Community
Magento Community
 
wdchoi
Jr. Member
 
Total Posts:  20
Joined:  2008-11-17
 

Hi, jakilcz

Ok, this is something seeming not easy at all, and pretty difficult to get completely at first… I mean that was for me.

Here is the magical class for that : Mage_Eav_Model_Entity_Attribute_Source_Table

Once you have the attribute object, you have to get its source, and then its options. Like this :

$seller $attrs['seller_confirmation'];
$options $seller->getSource()->getAllOptions();
then loop through $options to get the id with the text value.

You have to make sure $seller is a completely loaded attribute magento object (a select attribute, of course)

inversely, you could use

$option $seller->getSource()->getOptionText($id);
to get the text of a known id value.

That should do it.
PM me if you have problems with this.
David

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top