I use Magento

I've created a custom attribute and now I've been trying to get it in the cart page (template/checkout/cart.phtml:146-148) in this way




The key exists but the value is null.

I've looked and followed all steps at this link:


I've already cleaned cache and reindex all indexes.

This is the install script

$installer = $this;

$installer->addAttribute('catalog_product', 'vendor_id', array(
    'group'                     => 'General',
    'type'                      => 'int',
    'backend'                   => '',
    'frontend'                  => '',
    'label'                     => 'Vendor',
    'input'                     => 'select',
    'class'                     => '',
    'source'                    => 'mynamespace_mymodule/source_vendor',
    'global'                    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'                   => true,
    'required'                  => true,
    'user_defined'              => true,
    'default'                   => '',
    'searchable'                => true,
    'filterable'                => true,
    'comparable'                => true,
    'visible_on_front'          => true,
    'unique'                    => false,
    'apply_to'                  => 'simple',
    'is_configurable'           => false,
    'used_in_product_listing'   => true,

This is the upgrade script

$installer = new Mage_Sales_Model_Resource_Setup('core_setup');

$entities = array(
$options = array(
    'type'     => 'int',
    'visible'  => true,
    'required' => true
foreach ($entities as $entity) {
    $installer->addAttribute($entity, 'vendor_id', $options);

This is my config.xml

                    <vendor_id />



And this is the observer

class Mynamespace_Mymodule_Model_Observer extends Varien_Object
    public function salesQuoteItemSetVendorId($observer)
        $quoteItem = $observer->getQuoteItem();
        $product = $observer->getProduct();

        return $this;

On database side the attribute exists in eav_attribute and is assigned to two product in catalog_product_entity_int.

I don't know what to do to get it work!

Someone please suggest me some tests to find out why it does not work.

UPDATE 2015-11-04

If I print out the product data inside the observer file, the vendor_id is not present


Se now the question is why my custom attribute is missing in the product?

  • I assume that is just a part of your config.xml? You are missing <config> and <global> levels if not. Nov 3 '15 at 19:31
  • I omitted them in the code above but they are both present in my config file
    – nulele
    Nov 4 '15 at 6:29

You will need to create a field instead of attribute for the sales.

$installer->run("ALTER TABLE `{$installer->getTable('sales/quote_item')}` ADD `vendor_id` int(11) DEFAULT NULL;");
$installer->run("ALTER TABLE `{$installer->getTable('sales/order_item')}` ADD `vendor_id` int(11) DEFAULT NULL;");

Same way you will need to do it for order and order address table if you want to set data in those tables also.

In the latest Magento you will need to create field instead of sales attribute.

To get more information about it check this link

  • Hi and thank you! I've added your code to the install script and re-run everything. I can now see the new column in the sales_flat_quote_item table, but the column value is NULL. Anyway, I tried to force the value manually but in the cart page the vendor_id is still NULL!
    – nulele
    Nov 4 '15 at 6:09

Please add bellow code in config.xml


Also assigned vendor to product in admin side and clear cash

See screenshot : enter image description here

and (template/checkout/cart.phtml:146-148) try to display frontend:

echo $_item->getProduct()->getVendorId()

So... what I guiltily omitted was this line (already updated in the question) in the install script:

'source' => 'mynamespace_mymodule/source_vendor',

In fact my custom attribute is related to a custom model Vendor.

The problem was that my custom attribute was missing in the catalog_product_flat_X table, so a more specific search took me to this link:


As suggested I added these two functions in my Model/Source/Vendor.php:

public function getFlatColums()
    return array($this->getAttribute()->getAttributeCode() => array(
        'type'      => 'int',
        'unsigned'  => true,
        'is_null'   => true,
        'default'   => null,
        'extra'     => null

public function getFlatUpdateSelect($store)
    return Mage::getResourceSingleton('eav/entity_attribute')
        ->getFlatUpdateSelect($this->getAttribute(), $store);

I've re-run the install script, re-indexed all indexes and now the custom attribute appears in the product object inside the observer file and consequently in the quote item inside the checkout page!

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.