Magento 1.9 – Save Custom Data and Add New Cart Item Instead of Updating Quantity

cartmagento-1.9PHP

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,

checkout_cart_product_add_after

On this event you can call this updated function to show your product with new row,

public function saveProductTabData(Varien_Event_Observer $observer)
    {   
        $product = $observer->getEvent()->getProduct();
        $quote = Mage::getModel('checkout/cart')->getQuote();
        $quoteItem = $quote->getItemByProduct( $product );

        $productId = $product->getId();
        try {

            $data = Mage::app()->getRequest()->getPost();

            $customize_price = $data['customize_price'];
            $customize_data = $data['customize_data'];
            $customize_status = $data['customize_status'];
            $customize_description = $data['customize_description'];

            $quoteItem->setCustomizePrice($customize_price);
            $quoteItem->setCustomizeData($customize_data);
            $quoteItem->setCustomizeStatus($customize_status);
            $quoteItem->setCustomizeDescription($customize_description);
            $quoteItem->addOption(array(
                  "code" => "new_row_for_product",
                  "value" => serialize(array(time()))
            ));
            $quoteItem->save();
        }
        catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
    }