/** * Set shipping information to quote * * @param OrderInjectable $order * @throws \Exception */ protected function setShippingInformation(OrderInjectable $order) { if (!$order->hasData('shipping_method')) { return; } $url = $this->url . '/shipping-information'; list($carrier, $method) = explode('_', $order->getShippingMethod()); $address = $order->hasData('shipping_address_id') ? $order->getShippingAddressId() : $order->getBillingAddressId(); unset($address['default_billing']); unset($address['default_shipping']); foreach (array_keys($this->mappingData) as $key) { if (isset($address[$key])) { $address[$key] = $this->mappingData[$key][$address[$key]]; } } $data = ['addressInformation' => ['shippingAddress' => $address, 'shippingMethodCode' => $method, 'shippingCarrierCode' => $carrier]]; $this->webapiTransport->write($url, $data, WebapiDecorator::POST); $response = json_decode($this->webapiTransport->read(), true); $this->webapiTransport->close(); if (!isset($response['payment_methods'], $response['totals'])) { $this->eventManager->dispatchEvent(['webapi_failed'], [$response]); throw new \Exception('Could not set shipping method to quote!'); } }
/** * Set address to quote. * * @param OrderInjectable $order * @param string $addressType billing|shipping * @return void * @throws \Exception */ protected function setAddress(OrderInjectable $order, $addressType) { $url = $this->url . "/{$addressType}-address"; if ($addressType == 'billing') { $address = $order->getBillingAddressId(); } else { if (!$order->hasData('shipping_method')) { return; } $address = $order->hasData('shipping_address_id') ? $order->getShippingAddressId() : $order->getBillingAddressId(); } unset($address['default_billing']); unset($address['default_shipping']); foreach (array_keys($this->mappingData) as $key) { if (isset($address[$key])) { $address[$key] = $this->mappingData[$key][$address[$key]]; } } $data = ["address" => $address]; $this->webapiTransport->write($url, $data); $response = json_decode($this->webapiTransport->read(), true); $this->webapiTransport->close(); if (!is_numeric($response)) { $this->eventManager->dispatchEvent(['webapi_failed'], [$response]); throw new \Exception("Could not set {$addressType} addresss to quote!"); } }