1

Is there any way to store checkout custom field data to checkout session? If possible how can I retrieve data?

Created an event

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_onepage_controller_success_action">
        <observer name="checkout_onepage_observer" instance="Mageplaza\HelloWorld\Observer\TestObserver" />
    </event>
</config>

Observer code-

 <?php
    namespace Oye\Deliverydate\Model\Observer;
    use Magento\Framework\Event\Observer as EventObserver;
    use Magento\Framework\Event\ObserverInterface;

    /**
     * Class SaveDeliveryDateToOrderObserver
     * @package Oye\Deliverydate\Model\Observer
     */
    class SaveDeliveryDateToOrderObserver implements ObserverInterface
    {
        /**
         * @var \Magento\Framework\ObjectManagerInterface
         */
        protected $_objectManager;

        /**
         * @param \Magento\Framework\ObjectManagerInterface $objectmanager
         */
        public function __construct(\Magento\Framework\ObjectManagerInterface $objectmanager)
        {
            $this->_objectManager = $objectmanager;
        }

        /**
         * @param EventObserver $observer
         * @return $this
         */
        public function execute(EventObserver $observer)
        {
            $order = $observer->getOrder();
            $quoteRepository = $this->_objectManager->create('Magento\Quote\Model\QuoteRepository');
            $quote = $quoteRepository->get($order->getQuoteId());
            $order->setDeliveryDate( $quote->getDeliveryDate() );
            //echo $quote->getDeliveryDate();
           // exit();

        }

    }

In the observer last line I tried to echo deliverydate custom field value using echo $quote->getDeliveryDate(); but not getting that value. Can I use session to store these custom checkout field value?

8
  • give more details at which location (model/controller/block) you are and what you try to save in session. Oct 23, 2018 at 7:29
  • If you have captche the custom field data at Magento checkout page then it is too tough to set save the data at session attribute
    – Amit Bera
    Oct 23, 2018 at 8:02
  • Added More Details please check
    – Ragesh VP
    Oct 23, 2018 at 8:42
  • $quote->getDeliveryDate() will not show anything unless you have saved Delivery Date in quote table. Oct 23, 2018 at 8:48
  • saved in quote table also
    – Ragesh VP
    Oct 23, 2018 at 8:49

2 Answers 2

1
protected $_checkoutSession;
public function __construct(
    ....
    public function __construct(
    \Magento\Checkout\Model\Session $checkoutSession
) {

    $this->_checkoutSession = $checkoutSession;
}

public function getCheckoutSession() 
{
    return $this->_checkoutSession;
}

You set a value :

$this->getCheckoutSession()->setVarValue('someValueHere');

You get a value :

$this->getCheckoutSession()->getVarValue(); //someValueHere

Then you unset the value :

$this->getCheckoutSession()->unsVarValue();
0

To know about use of checkout session you should check below link -

http://blog.chapagain.com.np/magento-2-set-unset-get-session/

for more specific answer please share the exact event or requirement when you want to use checkout session.

3
  • I added a custom field in magento 2 checkout page and data saved in quote table.How can I store these data in a custom table after checkout success??
    – Ragesh VP
    Oct 23, 2018 at 7:29
  • you need to use any event observer in your module after order success like sales_order_place_after or checkout_submit_all_after and then there you can save value in your custom table. Oct 23, 2018 at 7:35
  • for your help check this link Oct 23, 2018 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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