Magento 2,下订单时调用外部Web服务



我一直在四处挖掘,但我似乎根本找不到我正在寻找的答案。

在 magento 2 中下订单时,我需要调用外部 Web 服务。我还需要在通话中使用订单信息。我根本不知道正确的道路是什么。以前有人这样做过吗?

我知道

答案很晚,但这是对我有用的解决方案,我已经为事件checkout_submit_all_after写了观察者。

在路径 [公司名称]/

[模块名称]/etc/events中创建文件.xml

<?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_submit_all_after">
        <observer name="push_orders_to_purchaser" instance="RLTSquareQuoteObserverPushOrdersToPurchaserApi" />
    </event>
</config>

现在在观察者([公司名称]/[模块名称]/观察者/PushOrdersToBuyerApi.php(

    <?php
    namespace RLTSquareQuoteObserver;
    use MagentoFrameworkEventObserver;
    use MagentoFrameworkEventObserverInterface;
    use MagentoFrameworkHTTPAdapterCurl;
    class PushOrdersToPurchaserApi implements ObserverInterface
    {
        const MOCK_API = 'https://yourwebsite.com/insertorders';
        protected $_request;
        protected $_webApiRequest;
        protected $_serviceOutputProcessor;
        protected $_zendClientFactory;

        public function __construct
        (
            MagentoFrameworkAppRequestInterface $request,
            MagentoFrameworkWebapiRestRequest $webApiRequest,
            MagentoFrameworkWebapiServiceOutputProcessor $serviceOutputProcessor,
            MagentoFrameworkHTTPZendClientFactory $zendClientFactory
        )
        {
            $this->_request = $request;
            $this->_webApiRequest = $webApiRequest;
            $this->_serviceOutputProcessor = $serviceOutputProcessor;
            $this->_zendClientFactory = $zendClientFactory;
        }
        public function execute(Observer $observer)
        {
            /**
             * @var MagentoSalesModelOrder[] $orders
             */
            $orders = $observer->getOrders();
            $encodedData = null;
            if ($orders) {
                $data = $this->_serviceOutputProcessor->convertValue($orders, 'MagentoSalesApiDataOrderInterface[]');
                $payload = Zend_Json::encode($data, true);
                $client = $this->_zendClientFactory->create();
                $client->setUri(self::MOCK_API);
                $client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
                $client->setRawData($payload);
                $response = $client->request(Zend_Http_Client::POST)->getBody();

   //play with response here...
            }
        }
    }

相关内容

  • 没有找到相关文章

最新更新