3

I am trying to save the payment related custom data in additional_information column through my custom module.can any one help me on this. Observer class

namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Api\Data\PaymentInterface;

class PaymentAssignData implements ObserverInterface {

    protected $_request;

    /**
     * @var ObjectManagerInterface
     */
    protected $_objectManager;

    /**
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     */
    public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager, \Psr\Log\LoggerInterface $logger, \Magento\Quote\Model\QuoteFactory $quoteFactory, \Magento\Framework\App\RequestInterface $request
    ) {
        //die(__METHOD__);
        $this->_objectManager = $objectManager;
        $this->logger = $logger;
        $this->quoteFactory = $quoteFactory;
        $this->_request = $request;
    }

    /**
     * customer register event handler
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer) {
        $order = $observer->getEvent()->getOrder();
        $addInfo = $order->getPayment()->getData();
        var_dump(json_encode($addInfo));
        exit;
        $quoteRepository = $this->_objectManager->create('Magento\Quote\Model\QuoteRepository');
        /** @var \Magento\Quote\Model\Quote $quote */
        var_dump(json_encode($observer));
        exit;
        $quote = $quoteRepository->get($order->getQuoteId());
        $order->setDeliveryDate($quote->getDeliveryDate());





        $order = $observer->getEvent()->getOrder();
        //$quote = $order->getQuote();
        $quoteId = $order->getQuoteId();
        $payment = $order->getPayment()->getData('additional_data');
        $quote = $this->quoteFactory->create()->load($quoteId);
        // $postData = $this->_request->getParam();;
        var_dump($payment);
        exit;
        // var_dump($quoteId);exit;

}
}

additional field in payment section.that fields are for all payment method. js file

  define(
        [
            'ko',
            'jquery',
            'uiComponent'
        ],
        function (ko, $, Component) {
            'use strict';
            return Component.extend({
                defaults: {
                    template: 'Vendor_Module/checkout/formdata-form',
                    taxCode: '',
                    taxState: ''
                },
                initialize: function () {
                    this._super()
                            .observe(
                                    [   'taxCode',
                                        'taxState'
                                    ]
                                    );
                    return this;
                },
                getData: function () {
                    return {
                        "additional_data": {
                            'tax_code': this.taxCode(),
                            'tax_state': this.taxState()
                        }
                    };
                }
            });
        }
);
  • Add new column in desired table through install script , and use plugin or event to insert data in that table . if you can more precise when you want to save i can tell you probable event as well – Vishwas Bhatnagar Nov 24 '16 at 10:18
  • which event should i use to save the data. – rajat kara Nov 24 '16 at 10:21
  • How to get the custom field data to the observer to set data. – rajat kara Nov 24 '16 at 10:22
  • When are you trying to save data ? – Vishwas Bhatnagar Nov 24 '16 at 10:24
  • i am using "sales_order_save_before" this event to saving the data but not getting the data in observer. – rajat kara Nov 24 '16 at 10:25
4

you can use sales_order_invoice_save_after(if you want to be sure the order is paid) or checkout_onepage_controller_success_action (after order success )

config.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_invoice_save_after">
    <observer name="inteceptafterinvoice" instance="Mycomapny\Inventoryrationing\Observer\Afterinvoice" />
</event>

Afterinvoice.php

class Afterinvoice implements ObserverInterface {

       public function execute(Observer $observer) 
       {
          $_event = $observer->getEvent();
          $_invoice = $_event->getInvoice();
          $_order = $_invoice->getOrder();
          $addInfo= $_order->getPayment()->getAdditionalInformation();
          [...]
          // your To insert data in another table
          [...]
        }
    }

For more info refer to this link

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.