0

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?

  • give more details at which location (model/controller/block) you are and what you try to save in session. – Hassan Ali Shahzad Oct 23 '18 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 '18 at 8:02
  • Added More Details please check – Ragesh VP Oct 23 '18 at 8:42
  • $quote->getDeliveryDate() will not show anything unless you have saved Delivery Date in quote table. – Shashank Kumrawat Oct 23 '18 at 8:48
  • saved in quote table also – Ragesh VP Oct 23 '18 at 8:49
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.

  • 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 '18 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. – Shashank Kumrawat Oct 23 '18 at 7:35
  • for your help check this link – Shashank Kumrawat Oct 23 '18 at 7:35
0
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();

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.