1

I create my custom attribute, but when I go show this with my method it doesn't work!

See was do... create my attribute..

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$config = array(
    'position' => 1,
    'required'=> 0,
    'label' => 'Height',
    'type' => 'int',
    'input' => 'text',
    'apply_to' => 'simple,bundle,grouped,configurable'
);

$setup->addAttribute('catalog_product', 'height' , $config);

and I get a list of items in checkout...

$items = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();

foreach($items as $item){

    echo $item->getSku() .'<br/>'; //just test... and all right!

    echo $item->getHeight() .'<br/>'; //return empty! or....
    echo $item->getData('height') .'<br/>';//return empty!
}

I set values in this attribute's fiels in my backend.

Thanks for help me!

mhatch
  • 4,441
  • 6
  • 36
  • 62
Matheus Gontijo
  • 1,178
  • 1
  • 12
  • 29

2 Answers2

6

Your attributes probably aren't being loaded by default. A cheat, without fixing the root problem of adding height to the collection's addAttributeToSelect() statement would be to load the product model again:

$product = Mage::getModel('catalog/product')->load($item->getProduct()->getId());
echo $product->getHeight();

This doesn't solve the root of the problem though, and fires off an additional database query.

I asked a similar question a couple of months ago regarding loading additional data which contains some more information, although more related to collection loading than individual models: Retrieving additional data from already loaded Magento models.

Community
  • 1
  • 1
Nick
  • 6,967
  • 2
  • 34
  • 56
  • Your cheat fixed my problem too, so I assume it has the same cause. That said, what does, "your attributes probably aren't being loaded by default" mean?? How do I load my attributes by default? – Protector one May 26 '14 at 09:59
0

Try echo $item->getProduct()->getHeight();

Alexandre
  • 3,088
  • 3
  • 34
  • 53