0

Get the values from magento2 checkout page but I need to pass into the rest api. How to pass the values.

*$params =   array( 'OrderLineId'=>$orderLineId,
                        'MaterialNo'=>$materialNo,
                        'PlanType'=>$planType,
                        'Qty'=>$qty,
                        'UnitPrice'=>$unitPrice,
                        'UnitTax'=>$UnitTax,
                        'DiscountRate'=>$DiscountRate,
                        'Discount'=>$Discount,
                        'OrderId'=>$orderId,
                        'StoreId'=>$storeId,
                        'PartnerId'=>  $partnerId,
                        'CustomerId'=> $customerId,
                        'OrderType'=> $orderType,
                        'OrderSource'=>  $orderSource,
                        'CurrId'=> $currId,
                        'UserName'=> $userName);
                        $data = json_encode($params);
$ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch,CURLOPT_HEADER);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
            $response = curl_exec($ch);
            curl_close($ch);

         } catch (Exception $e) {
             echo $e->getMessage(); exit;
              }

Can you anyone please help me.

  • When you need to send the data? Before the order is finished, when the checkout page is loaded? Depending on your answer, the best approach would be creating a plugin. There you could put your business logic. – Thiago Lima Feb 11 '18 at 11:17
  • Before place an order I fetch the data through the observer. But I ran this page not posting to API. – venkata prasad Feb 11 '18 at 14:29
  • then you can listen to the event checkout_type_onepage_save_order_after it dispaches ['order' => $order, 'quote' => $this->getQuote()] At your observer you could use $order = $observer->getEvent()->getOrder() in order to get the order. – Thiago Lima Feb 11 '18 at 14:42
  • values are not passing to rest API from this data – venkata prasad Feb 12 '18 at 8:44
  • You may be doing something wrong then. – Thiago Lima Feb 12 '18 at 9:02
0

If you are using a observer, you could listen to event checkout_type_onepage_save_order_after

Take a look into vendor/magento/module-downloadable/etc/events.xml

You will see:

 <event name="sales_model_service_quote_submit_success">
    <observer name="checkout_type_onepage_save_order_after" instance="Magento\Downloadable\Observer\SetHasDownloadableProductsObserver" />
</event>

You can implement you logic the same way that Magento\Downloadable\Observer\SetHasDownloadableProductsObserver class does. Check the method execute

/**
 * Set checkout session flag if order has downloadable product(s)
 *
 * @param \Magento\Framework\Event\Observer $observer
 * @return $this
 */
public function execute(\Magento\Framework\Event\Observer $observer)
{
    if (!$this->_checkoutSession->getHasDownloadableProducts()) {
        $order = $observer->getEvent()->getOrder();
        foreach ($order->getAllItems() as $item) {
            /* @var $item \Magento\Sales\Model\Order\Item */
            if ($item->getProductType() == \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE
                || $item->getRealProductType() == \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE
                || $item->getProductOptionByCode(
                    'is_downloadable'
                )
            ) {
                $this->_checkoutSession->setHasDownloadableProducts(true);
                break;
            }
        }
    }

    return $this;
}

You just need to adapt the code to your needs. Most of your work is copy and paste.

:)

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.