Magento 1.9 – Fix Custom Product Attribute is Null

attributescartinstall-scriptmagento-1.9

I use Magento 1.9.2.1.

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

Zend_Debug::dump($_item->getProduct()->getData())

or

Zend_Debug::dump($_item->getData())

The key exists but the value is null.

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

http://www.atwix.com/magento/custom-product-attribute-quote-order-item/

I've already cleaned cache and reindex all indexes.

This is the install script

$installer = $this;
$installer->startSetup();

$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,
));
$installer->endSetup();

This is the upgrade script

$installer = new Mage_Sales_Model_Resource_Setup('core_setup');

$entities = array(
    'quote',
    'quote_address',
    'quote_item',
    'quote_address_item',
    'order',
    'order_item',
);
$options = array(
    'type'     => 'int',
    'visible'  => true,
    'required' => true
);
foreach ($entities as $entity) {
    $installer->addAttribute($entity, 'vendor_id', $options);
}
$installer->endSetup();

This is my config.xml

    <sales>
        <quote>
            <item>
                <product_attributes>
                    <vendor_id />
                </product_attributes>
            </item>
        </quote>
    </sales>

    <fieldsets>
        <sales_convert_quote_item>
            <vendor_id>
                <to_order_item>*</to_order_item>
            </vendor_id>
        </sales_convert_quote_item>
        <sales_convert_order_item>
            <vendor_id>
                <to_quote_item>*</to_quote_item>
            </vendor_id>
        </sales_convert_order_item>
    </fieldsets>

    <events>
        <sales_quote_item_set_product>
            <observers>
                <mynamespace_mymodule>
                    <class>mynamespace_mymodule/observer</class>
                    <method>salesQuoteItemSetVendorId</method>
                </mynamespace_mymodule>
            </observers>
        </sales_quote_item_set_product>
    </events>

And this is the observer

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

        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

Zend_Debug::dump($observer->getProduct()->getData());

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

Best Answer

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

Related Topic