cancel
Showing results for 
Search instead for 
Did you mean: 

Save custom data to ('checktout/session') when product added to cart and remove when removed

SOLVED

Save custom data to ('checktout/session') when product added to cart and remove when removed

So i need to add custom data for each product when customer adds it to card and remove it when customer removes from cart.
What i have done so far was creating observer that fires up when customer adds product to cart, but it doesnt save my data in global ('checkout/session'). In observer i can see my custom data saved. 
This is the code in observer:

        $quoteId = Mage::getSingleton('checkout/session')->getQuote()->getEntityId();
        $quote = Mage::getModel('sales/quote')->load($quoteId);

        $carDetails = Mage::getSingleton('core/session')->getCarDetails();
        if(empty($carDetails)){
            $carDetails = 'No Car in Search';
        }

        $quoteItems = $quote->getAllItems();

        foreach ($quoteItems as $quoteItem){
$product = $quoteItem->getProduct();
$quoteItems = $product->setCarDetails($carDetails);
echo'<pre>';print_r($product->getId()); echo'<pre>';print_r($product->getCarId());die(); }

Any help or suggestion would be greatly appreciated. 
If someone knows better way of doing this, also greatly appreciated.


p.s. i need to save this data later on as extra or note to Invoice or Order.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Save custom data to ('checktout/session') when product added to cart and remove when removed

Hello @paganini 

 

Please follow the below steps:

  1. Create config.xml file at app/code/local/Vendor/Extension/etc/ folder
    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <modules>
            <Vendor_Extension>
                <version>1.0.0</version>
            </Vendor_Extension>
        </modules>
        <global>
            <models>
                <extension>
                    <class>Vendor_Extension_Model</class>
                </extension>
            </models>
            <events>
                <checkout_cart_product_add_after>
                    <observers>
                        <Vendor_Extension_Model_Observer>
                            <type>singleton</type>
                            <class>Vendor_Extension_Model_Observer</class>
                            <method>checkout_cart_product_add_after</method>
                        </Vendor_Extension_Model_Observer>
                    </observers>
                </checkout_cart_product_add_after>
    
                <sales_model_service_quote_submit_before>
                    <observers>
                        <Vendor_Extension_Model_Observer_Set_Order>
                            <type>singleton</type>
                            <class>Vendor_Extension_Model_Observer</class>
                            <method>setAdditionalDataToOrder</method>
                        </Vendor_Extension_Model_Observer_Set_Order>
                    </observers>
                </sales_model_service_quote_submit_before>
            </events>
        </global>
    </config>
  2. Create Observer.php file at app/code/local/Vendor/Extension/Model/ folder
    <?php
    
    class Vendor_Extension_Model_Observer
    {
        public function checkout_cart_product_add_after($observer)
        {
            $event = $observer->getEvent();
            $quoteItem = $event->getQuoteItem();
    
            $messageOption = array(
                'label' => 'Message',
                'option_value' => 'Custom Value',
                'value' => 'Custom Value',
                'print_value' => 'Custom Value',
            );
    
            $value = array('message' => $messageOption);
            $value = serialize($value);
            $quoteItem->addOption(array('code' => 'additional_options', 'product_id' => $quoteItem->getProductId(), 'value' => $value));
        }
    
        public function setAdditionalDataToOrder($observer)
        {
            try {
                $quote = $observer->getEvent()->getQuote();
                $order = $observer->getEvent()->getOrder();
                $quoteItems = [];
    
                foreach ($quote->getAllVisibleItems() as $quoteItem) {
                    $quoteItems[$quoteItem->getId()] = $quoteItem;
                }
    
                foreach ($order->getAllVisibleItems() as $orderItem) {
    
                    $quoteItemId = $orderItem->getQuoteItemId();
                    $quoteItem = $quoteItems[$quoteItemId];
    
                    $additionalOptions = $quoteItem->getOptionByCode('additional_options');
    
                    if (count($additionalOptions) > 0) {
                        $options = $orderItem->getProductOptions();
                        $options['additional_options'] = unserialize($additionalOptions->getValue());
                        $orderItem->setProductOptions($options);
                    }
    
                }
            } catch (\Exception $e) {
                Mage::log($e->getMessage());
            }
        }
    }

I hope it helps.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

View solution in original post

2 REPLIES 2

Re: Save custom data to ('checktout/session') when product added to cart and remove when removed

Hello @paganini 

 

Please follow the below steps:

  1. Create config.xml file at app/code/local/Vendor/Extension/etc/ folder
    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <modules>
            <Vendor_Extension>
                <version>1.0.0</version>
            </Vendor_Extension>
        </modules>
        <global>
            <models>
                <extension>
                    <class>Vendor_Extension_Model</class>
                </extension>
            </models>
            <events>
                <checkout_cart_product_add_after>
                    <observers>
                        <Vendor_Extension_Model_Observer>
                            <type>singleton</type>
                            <class>Vendor_Extension_Model_Observer</class>
                            <method>checkout_cart_product_add_after</method>
                        </Vendor_Extension_Model_Observer>
                    </observers>
                </checkout_cart_product_add_after>
    
                <sales_model_service_quote_submit_before>
                    <observers>
                        <Vendor_Extension_Model_Observer_Set_Order>
                            <type>singleton</type>
                            <class>Vendor_Extension_Model_Observer</class>
                            <method>setAdditionalDataToOrder</method>
                        </Vendor_Extension_Model_Observer_Set_Order>
                    </observers>
                </sales_model_service_quote_submit_before>
            </events>
        </global>
    </config>
  2. Create Observer.php file at app/code/local/Vendor/Extension/Model/ folder
    <?php
    
    class Vendor_Extension_Model_Observer
    {
        public function checkout_cart_product_add_after($observer)
        {
            $event = $observer->getEvent();
            $quoteItem = $event->getQuoteItem();
    
            $messageOption = array(
                'label' => 'Message',
                'option_value' => 'Custom Value',
                'value' => 'Custom Value',
                'print_value' => 'Custom Value',
            );
    
            $value = array('message' => $messageOption);
            $value = serialize($value);
            $quoteItem->addOption(array('code' => 'additional_options', 'product_id' => $quoteItem->getProductId(), 'value' => $value));
        }
    
        public function setAdditionalDataToOrder($observer)
        {
            try {
                $quote = $observer->getEvent()->getQuote();
                $order = $observer->getEvent()->getOrder();
                $quoteItems = [];
    
                foreach ($quote->getAllVisibleItems() as $quoteItem) {
                    $quoteItems[$quoteItem->getId()] = $quoteItem;
                }
    
                foreach ($order->getAllVisibleItems() as $orderItem) {
    
                    $quoteItemId = $orderItem->getQuoteItemId();
                    $quoteItem = $quoteItems[$quoteItemId];
    
                    $additionalOptions = $quoteItem->getOptionByCode('additional_options');
    
                    if (count($additionalOptions) > 0) {
                        $options = $orderItem->getProductOptions();
                        $options['additional_options'] = unserialize($additionalOptions->getValue());
                        $orderItem->setProductOptions($options);
                    }
    
                }
            } catch (\Exception $e) {
                Mage::log($e->getMessage());
            }
        }
    }

I hope it helps.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Save custom data to ('checktout/session') when product added to cart and remove when removed

Thank you very much, this was exactly what i was looking for!
You saved the day!