1

I want a json file of order data to be created in root directory after the checkout is completed. I have been searching this for hours now but still luck.

  • you will have to code this completely on your own. searching for a solution won't help since this a very odd requirment. – Philipp Sander Mar 11 at 10:28
1

Create an observer for the event checkout_type_onepage_save_order_after. That event is called after the order is successfully saved at the end of the checkout.

In the observer you will have two objects: the order and the related quote. Therefore you can get all the data you need in that method, serialize it as json and save it wherever you need.

Here is an example how that observer should be built:

protected $jsonHelper;

public function __construct(\Magento\Framework\Json\Helper\Data $jsonHelper)
{
    $this->jsonHelper = $jsonHelper;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order = $observer->getEvent()->getOrder();

    $orderData = [];

    //example order data
    $orderData['increment_id'] = $order->getIncrementId();

    //....
    //get order items information
    $orderData['items'] = [];
    foreach ($order->getAllItems() as $item) {
        //example order item data
        $orderData['items'][] = ['sku' => $items->getSku(), 'qty' => $item->getQty()]
    }

    //encode the data as json
    $jsonOrderData = $this->jsonHelper->jsonEncode($orderData);

    //add your save to file logic here and use $jsonOrderData
}

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.