3

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?

  • I assume that is just a part of your config.xml? You are missing <config> and <global> levels if not. – TJ Gamble at Jamersan 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
0

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
0

Please add bellow code in config.xml

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

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()
0

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:

http://www.dconstructing.com/2012/03/14/custom-product-attributes-and-flat-database-tables-in-magento

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.