Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Data set by $item->setData(…) is not persistent
 
Andre D
Jr. Member
 
Total Posts:  13
Joined:  2008-10-29
 

I’m trying to store a piece of data in an item that I want to use later in the checkout process. I’m trying to set the data this way:

// $item is a Mage_Sales_Model_Quote_Item
$value 'some value';
$item->setData('some_key'$value);
$item->save();
Then later I expect to be able to retrieve it this way:
$value $item->getData('some_key');
// $value is NULL, the key was not found

Can anyone spot what I’m doing wrong?

 
Magento Community Magento Community
Magento Community
Magento Community
 
magedev
Sr. Member
 
Avatar
Total Posts:  117
Joined:  2009-06-01
 

Could you specify how you retrieve the item later in your code? If for example you get it from collection it’s possible the ‘some_key’ attribute has not been loaded (not specified in database query).

 
Magento Community Magento Community
Magento Community
Magento Community
 
Andre D
Jr. Member
 
Total Posts:  13
Joined:  2008-10-29
 

Thank you for your reply. I’ve tried it several ways. Here is one example:

// Set data example
// In Mage_Checkout_Model_Cart
public function updateItems($data)
{
    
//...
    
$item $this->getQuote()->getItemById($itemId);
    
$item->setData('some_key''some value');
    
$item->save();
    
//...
}
Note: In reality I’m extending the Mage_Checkout_Model_Cart class in a custom module.
// Get data example
// In /app/design/frontend/<store>/default/template/checkout/cart/item/default.phtml
$_item $this->getItem();
$value $_item->getData('some_key');
Before updating the cart, I expect the value to be NULL, but afterwords I expect it to be “some value” but it is not.
 
Magento Community Magento Community
Magento Community
Magento Community
 
Andre D
Jr. Member
 
Total Posts:  13
Joined:  2008-10-29
 

The data I want to store is ephemeral and would ideally be stored in the session; it really doesn’t need to be in the database. I could just write it to the session myself, but I want to access it as a property of the item objects.

The easy solution I’ve gone with is to store the data as a custom item option value like so:

// Set data
$value 'some value';
if (
$itemOption $item->getOptionByCode($optionCode)) {
    $itemOption
->setValue($value);
    
$itemOption->save();
else {
    $item
->addOption(array(
        
'product_id' => $item->getProductId(),
        
'product' => $item->getProduct(),
        
'code' => 'some_key',
        
'value' => $value
    
));
    
$item->save();
}
// Get data
$value $item->getOptionByCode('some_key')->getValue();
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top