0

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.

  • if those products were same why you want them as a seprate product – Murtuza Zabuawala Jan 4 '17 at 9:23
  • If you added same product, then magento always update qty into cart not added as seprate product. Specify what you try to archive, so other can help you into that way. – Dhiren Vasoya Jan 4 '17 at 9:27
  • Product is same but customization data and price of the product is different every time. – Chetan Khandla Jan 4 '17 at 9:30
1

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());
        }
    }
  • Have you tried this in Magento 2 – Mandar Parbate Nov 15 '18 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.