I want to save custom data in to cart item and add new cart item instead of update quantity when product add to cart in Magento.
I have added below code in Observer.php file
public function checkoutCartProductAddAfter(Varien_Event_Observer $observer){
try {
$data = $this->_getRequest()->getPost();
$item = $observer->getQuoteItem();
$product = $item->getProduct();
if(isset($data['customize_data'])){
$results['data'] = $data['customize_data'];
$productModel = Mage::helper('productcustomizer')->getCustomizeData($product->getId());
$results['price'] = $productModel->getCustomizePrice();
}
if(isset($data['customize_image'])){
$file_name = $item->getProduct()->getId()."-".time()."-".rand(0,999).'-'.$item->getProduct()->getUrlKey();
$path = Mage::getBaseDir()."/media/productcustomizer/customize_image/";
$url = Mage::getBaseUrl()."media/productcustomizer/customize_image/";
$new_file_name = Mage::helper('productcustomizer')->save_base64_image($data['customize_image'],$file_name, $path);
$image_data = array(
'url' => $url.$new_file_name,
'path' => $path,
'name' => $new_file_name
);
$results['image'] = $image_data;
}
if(isset($results)){
$item->addOption(array(
"product_id" => $product->getId(),
"product" => $product,
"code" => "customizer_data",
"value" => serialize($results)
));
if(!empty($results['price']) ){
$finalPrice = $product->getPrice();
$qty = $item->getQty();
$price = ($finalPrice + $results['price']) * $qty;
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);
}
$item->collectTotals()->save();
}
}
catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
It is saving custom data into cart item but when i add same product in to cart then it is updating quantity. I want to add new item instead of update quantity.
I want to add separately because "customization data" and "price" of the product is different every time when add item into cart.
Best Answer
You can use this event to meet your requirement,
On this event you can call this updated function to show your product with new row,