Tariq Aziz
Tariq Aziz

Reputation: 798

Magento set the new attribute value

I have created a new IMEI attribute of type textarea for all products, see from the image. Can anyone point out a function to update the its value. I have the code like the following.

$this belogns to Mage_Sales_Model_Order.

    foreach ($this->getAllItems() as $item) {

                 $item->setImei('123');

             $item->save();
             echo $item->getImei();
    }

I am getting 123 from the last statement but when I am viewing from admin. Its not changing there. Also in which table the attribute and value will be stored, So I can debug from there.

enter image description here

Upvotes: 2

Views: 12403

Answers (3)

Hayden Thring
Hayden Thring

Reputation: 1810

heres how you do it differently: (color = attribute name, red = attribute value id)

starts assuming you already have $product available.

$attr = $product->getResource()->getAttribute('color');

            if ($attr->usesSource()) {

               $avid = $attr->getSource()->getOptionId('red');

               $product->setData('color', $avid);
               $product->save();
            }

Upvotes: 0

MagePal Extensions
MagePal Extensions

Reputation: 17656

What class is $this->getAllItems() is it Mage_Catalog_Model_Product?

If it not Mage_Catalog_Model_Product then load the product by id and save the product

foreach ($this->getAllItems() as $item) {
     $product = Mage::getModel('catalog/product')->load($item->getId() or $item->getProductId())
     $product->setImei($product->getImei() . '123');

     $product->save();   
}

Upvotes: 2

Oleg Ishenko
Oleg Ishenko

Reputation: 2233

The values of catalog product attributes of type text are stored in the table catalog_product_entity_text. An SQL would be

select * from catalog_product_entity_text where attribute_id = {insert your attribute id} and entity_id = {insert your product id}

The query will return results for every store view in the system.

The reason why you do not see a change to the attribute in the backend is probably because the new value is set for a different website/store than the one loaded in the backend.

You are already using a correct way to set the attribute value assuming $item is of type Mage_Catalog_Model_Product:

$item->setImei('123');
$item->save();

Upvotes: 1

Related Questions