Get Product Attribute Value at Cart Page Changed at Product Detailed Page

addtocartattributescheckoutmagento-1.9

I am developing one extension. I have created attribute at backend and assigned to product. And I am changing that product attribute while adding to cart and I am fetching same on checkout/cart page but I am not able to get same value on checkout/cart page.

I am storing this product value in checkout/cart model like this:

//saving product to cart
$product_id = Mage::app()->getRequest()->getPost('id'); 
        $qty = Mage::app()->getRequest()->getPost('qty'); 
        $product = Mage::getModel('catalog/product')->load($product_id);
        $product->setCanImage('wasdwasdwasd');

$cart = Mage::getModel('checkout/cart');
        $cart->init();
        $cart->addProduct($product, array('qty' => $qty));
        $cart->save();
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
        Mage::getSingleton('core/session')->addSuccess('Product added successfully');

And I can retrieve my attribute value at same page like following code but I am not able to get it on checkout/cart page.

$cartQuote = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();
        foreach ($cartQuote as $item) {
            $productId = $item->getProduct()->getId();
            $productPrice = $item->getProduct()->getPrice();
            $item->getProduct()->getCanImage();//I can get my value here
        }

And I have used same code which is above but I am not getting value from checkout/cart quote.

And I don't want to store this value in database because it will create trouble if many user will add this product dynamically so I think quote is good place to keep data for product.

Please suggest me solution.

Best Answer

First , $product->setCanImage('wasdwasdwasd');does not save the value at model object until use $product->save();

So you could not get $item->getProduct()->getCanImage() data as $item->getProduct()->getCanImage() is Object of Mage_Catalog_Model_Product.

One thing you can do save the value in session variable then use it at checkout/cart page.

Concept is that,that session variable's value is saved as array with format like.

$ValueinArray=array('product_id_1'=>$value,product_id_2'=>$value2,product_id_3'=>$value3);

Mage::getSingleton( 'customer/session' )->setCanImage($ValueinArray);

Then on cart page you need to call that variable value by Mage::getSingleton( 'customer/session' )->getCanImage()

and match with product index