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

Getting an Attribute Value ? 
 
kohril
Member
 
Total Posts:  44
Joined:  2007-12-23
Lille, northernFrance
 

I created a system attribute and added it to the Default set in a custom group. This attribute’s value is chosen with a Yes/No dropdown.
This attribute’s source model is : eav/entity_attribute_source_boolean.

In the backend, when editing a product, I can see my attribute, select a value and save it. It is well saved.

However, I am trying to retrieve this attribute’s value but I can’t find the correct way to do so.

I tried :

$this->getProduct()->getAttributeText('personnalisable');

It always returns “No” even if “yes” value is saved and editable from the backend.

$attributes $this->getProduct()->getAttributes();
$att $attributes['personnalisable'];
$att->getFrontend()->getValue($this->getProduct());

Same problem, always “No” ...

Any idea ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Is it possible that you have multiple store view, this attribute has scope Store View and you’re saving it only in Default Values, while the specific store value remains unchanged?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Also please debug that $this->getProduct()->getPersonnalisable() value is set to “1”;

 
Magento Community Magento Community
Magento Community
Magento Community
 
kohril
Member
 
Total Posts:  44
Joined:  2007-12-23
Lille, northernFrance
 

attribute scope is global

I tried $this->getProduct()->getPersonnalisable() and I did not get any result ...

 
Magento Community Magento Community
Magento Community
Magento Community
 
kohril
Member
 
Total Posts:  44
Joined:  2007-12-23
Lille, northernFrance
 

Here is the code of the setup I use to add my attribute in the Db :

class PersoJMB_Perso_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup{
    
    
public function addPersonnalisableSystemAttribute(){

        $entityTypeId
="catalog_product";
        
$setId=4;
        
$codeAttributPersonnalisation "personnalisable";
        
$groupName="Personnalisation";

        
$this->_createPersonnalisableSystemAttribute($entityTypeId$codeAttributPersonnalisation);
                
        
parent::addAttributeGroup($entityTypeId,$setId,$groupName);
        
        
$this->_addAttributeToAttributeGroup($entityTypeId$setId$groupName$codeAttributPersonnalisation);
    
}
    
    
protected function _createPersonnalisableSystemAttribute($entityTypeId$code){
        
        $attr 
= array(
            
'type'                => 'int',
            
'input'                => 'select',
            
'label'                => 'Personnalisable',
            
'source'            => 'eav/entity_attribute_source_boolean',
            
'visible'            => 0,
            
'required'            => 1,
            
'default'            => 0,
            
'is_configurable'       => 0,
            
'position'              => 1
        
);
        
        
parent::addAttribute($entityTypeId$code$attr);
    
}
    
    
/**
     * @param string|int $entityTypeId
     * @param int $setId
     * @param string|int $groupId
     * @param string|int $attributeId
     */
    
protected function _addAttributeToAttributeGroup($entityTypeId$setId$groupId$attributeCode){
        $groupId 
parent::getAttributeGroupId($entityTypeId$setId$groupId);
        
$attributeId parent::getAttributeId($entityTypeId$attributeCode);
        
parent::addAttributeToSet($entityTypeId$setId$groupId$attributeId);
    
}
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
kohril
Member
 
Total Posts:  44
Joined:  2007-12-23
Lille, northernFrance
 

And here is my attribute’s record in eav_attribute table :

attribute_id           |           578 |
|
entity_type_id        |               |
attribute_code     personnalisable |
attribute_model  |  NULL            |
[backend_model  |    | 
backend_type      int          |
backend_table    |               |
frontend_model  |                | 
frontend_input     |select         
frontend_label     |Personnalisable |
frontend_class    NULL           |
source_model    eav/entity_attribute_source_boolean |
is_global              |         |
is_visible              |     |
is_required           |           |
is_user_defined  |               |
default_value       |0             |
is_searchable     |               |
is_filterable          |     
is_comparable   |            |
is_visible_on_front |                   
is_unique             |        |
is_configurable   |               |
apply_to                |      |
position                 |       |
note                       |       | 
is_visible_in_advanced_search |             |
 
Magento Community Magento Community
Magento Community
Magento Community
 
kohril
Member
 
Total Posts:  44
Joined:  2007-12-23
Lille, northernFrance
 

In this post, I will add my tests’ results.

- in catalog_product_entity_int, the value 0 (no) and 1 (yes) is well saved when changing the attribute’s value from the backend
- this->getProduct()->getData(’personnalisable’) returns nothing
- Instead of using model eav/entity_attribute_source_boolean, I tried making my own class, my baseline was the “status” attribute.

Here is the class code :

class PersoJMB_Perso_Model_Product_Personnalisable extends Varien_Object
{
    
const STATUS_ENABLED    1;
    const 
STATUS_DISABLED   2;
    const 
LBL_COMBO_SELECTION 'Sélectionnez une valeur';
    const 
LBL_COMBO_ENABLED 'Oui';
    const 
LBL_COMBO_DISABLED 'Non';
    

    static public function 
getOptionArray()
    
{
        
return array(
            
self::STATUS_ENABLED    => Mage::helper('perso')->__(self::LBL_COMBO_ENABLED),
            
self::STATUS_DISABLED   => Mage::helper('perso')->__(self::LBL_COMBO_DISABLED)
        );
    
}

    
static public function getAllOption()
    
{
        $options 
self::getOptionArray();
        
array_unshift($options, array('value'=>'''label'=>''));
        return 
$options;
    
}

    
static public function getAllOptions()
    
{
        $res 
= array();
        
$res[] = array('value'=>'''label'=> Mage::helper('perso')->__(self::LBL_COMBO_SELECTION));
        foreach (
self::getOptionArray() as $index => $value{
            $res[] 
= array(
               
'value' => $index,
               
'label' => $value
            
);
        
}
        
return $res;
    
}

    
static public function getOptionText($optionId)
    
{
        $options 
self::getOptionArray();
        return isset(
$options[$optionId]) ? $options[$optionId] null;
    
}
}

Again, values are weel saved in admin backend but the results are the same :

- this->getProduct()->getAttributeText(’personnalisable’) returns nothing
- this->getProduct()->getData(’personnalisable’) returns nothing
-
$attributes = $this->getProduct()->getAttributes();
$att = $attributes[’personnalisable’];
$att->getFrontend()->getValue($this->getProduct());

returns No

Which is really stange as in the class I set labels “Oui” and “Non” instead “yes” or “no”.
I do not understand where the “No” label comes from.

 
Magento Community Magento Community
Magento Community
Magento Community
 
kohril
Member
 
Total Posts:  44
Joined:  2007-12-23
Lille, northernFrance
 

Apparently, the main problem is that this->getProduct()->getData(’personnalisable’) returns no result.

How it is possible to make an attribute available from the _data array ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
kohril
Member
 
Total Posts:  44
Joined:  2007-12-23
Lille, northernFrance
 

Hope this gonna help someone ...

Apparently, default behaviour implies that all attributes are not loaded. So, after calling the load method like that :

this->getProduct()->load($this->getProduct())

All methods mentionned above returns the good value.

Enjoy

 
Magento Community Magento Community
Magento Community
Magento Community
 
Hinz
Jr. Member
 
Total Posts:  1
Joined:  2012-08-15
 

I had the same problem in Magento 1.8 Community (german)
I needed a yes/no value but an attribute from type YES/NO did not work, always returned \"Nein\" in the list view
after reading a lot I succeeded:
made an attribute from type \’drop down\’
gave it the option values \’Nein\’ as default and \’Ja\’
Property \"In Kategorieansicht verwenden\” (something like use in category view MUST be set to yes)
Now <?php echo $_product->getAttributeText(\'preisaufanfrage\') ?> in list.phtml (around row 61 before if($_product->isSaleable())
gave the correct answer.
All the other methods using yes/No List or other things did not work at all.

Hope this helps someone as I also came here searching for an answer
Uwe

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