/** * Prepare and do request to shipment * * @param Shipment $orderShipment * @return \Magento\Framework\DataObject * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function requestToShipment(Shipment $orderShipment) { $admin = $this->_authSession->getUser(); $order = $orderShipment->getOrder(); $shippingMethod = $order->getShippingMethod(true); $shipmentStoreId = $orderShipment->getStoreId(); $shipmentCarrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode()); $baseCurrencyCode = $this->_storeManager->getStore($shipmentStoreId)->getBaseCurrencyCode(); if (!$shipmentCarrier) { throw new LocalizedException(__('Invalid carrier: %1', $shippingMethod->getCarrierCode())); } $shipperRegionCode = $this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_REGION_ID, ScopeInterface::SCOPE_STORE, $shipmentStoreId); if (is_numeric($shipperRegionCode)) { $shipperRegionCode = $this->_regionFactory->create()->load($shipperRegionCode)->getCode(); } $originStreet1 = $this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ADDRESS1, ScopeInterface::SCOPE_STORE, $shipmentStoreId); $storeInfo = new DataObject((array) $this->_scopeConfig->getValue('general/store_information', ScopeInterface::SCOPE_STORE, $shipmentStoreId)); if (!$admin->getFirstname() || !$admin->getLastname() || !$storeInfo->getName() || !$storeInfo->getPhone() || !$originStreet1 || !$shipperRegionCode || !$this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_CITY, ScopeInterface::SCOPE_STORE, $shipmentStoreId) || !$this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ZIP, ScopeInterface::SCOPE_STORE, $shipmentStoreId) || !$this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_COUNTRY_ID, ScopeInterface::SCOPE_STORE, $shipmentStoreId)) { throw new LocalizedException(__('We don\'t have enough information to create shipping labels. Please make sure your store information and settings are complete.')); } /** @var $request \Magento\Shipping\Model\Shipment\Request */ $request = $this->_shipmentRequestFactory->create(); $request->setOrderShipment($orderShipment); $address = $order->getShippingAddress(); $this->setShipperDetails($request, $admin, $storeInfo, $shipmentStoreId, $shipperRegionCode, $originStreet1); $this->setRecipientDetails($request, $address); $request->setShippingMethod($shippingMethod->getMethod()); $request->setPackageWeight($order->getWeight()); $request->setPackages($orderShipment->getPackages()); $request->setBaseCurrencyCode($baseCurrencyCode); $request->setStoreId($shipmentStoreId); return $shipmentCarrier->requestToShipment($request); }
/** * Retrieve tracking url with params * * @param string $key * @param \Magento\Sales\Model\Order|\Magento\Sales\Model\Order\Shipment|\Magento\Sales\Model\Order\Shipment\Track $model * @param string $method Optional - method of a model to get id * @return string */ protected function _getTrackingUrl($key, $model, $method = 'getId') { $urlPart = "{$key}:{$model->{$method}()}:{$model->getProtectCode()}"; $params = ['_direct' => 'shipping/tracking/popup', '_query' => ['hash' => $this->urlEncoder->encode($urlPart)]]; $storeModel = $this->_storeManager->getStore($model->getStoreId()); return $storeModel->getUrl('', $params); }
/** * Prepare and do request to shipment * * @param Shipment $orderShipment * @return \Magento\Framework\Object * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function requestToShipment(Shipment $orderShipment) { $admin = $this->_authSession->getUser(); $order = $orderShipment->getOrder(); $address = $order->getShippingAddress(); $shippingMethod = $order->getShippingMethod(true); $shipmentStoreId = $orderShipment->getStoreId(); $shipmentCarrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode()); $baseCurrencyCode = $this->_storeManager->getStore($shipmentStoreId)->getBaseCurrencyCode(); if (!$shipmentCarrier) { throw new \Magento\Framework\Exception\LocalizedException(__('Invalid carrier: %1', $shippingMethod->getCarrierCode())); } $shipperRegionCode = $this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_REGION_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId); if (is_numeric($shipperRegionCode)) { $shipperRegionCode = $this->_regionFactory->create()->load($shipperRegionCode)->getCode(); } $recipientRegionCode = $this->_regionFactory->create()->load($address->getRegionId())->getCode(); $originStreet1 = $this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ADDRESS1, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId); $originStreet2 = $this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ADDRESS2, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId); $storeInfo = new \Magento\Framework\Object((array) $this->_scopeConfig->getValue('general/store_information', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId)); if (!$admin->getFirstname() || !$admin->getLastname() || !$storeInfo->getName() || !$storeInfo->getPhone() || !$originStreet1 || !$shipperRegionCode || !$this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_CITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId) || !$this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ZIP, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId) || !$this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_COUNTRY_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId)) { throw new \Magento\Framework\Exception\LocalizedException(__('We don\'t have enough information to create shipping labels. Please make sure your store information and settings are complete.')); } /** @var $request \Magento\Shipping\Model\Shipment\Request */ $request = $this->_shipmentRequestFactory->create(); $request->setOrderShipment($orderShipment); $request->setShipperContactPersonName($admin->getName()); $request->setShipperContactPersonFirstName($admin->getFirstname()); $request->setShipperContactPersonLastName($admin->getLastname()); $request->setShipperContactCompanyName($storeInfo->getName()); $request->setShipperContactPhoneNumber($storeInfo->getPhone()); $request->setShipperEmail($admin->getEmail()); $request->setShipperAddressStreet(trim($originStreet1 . ' ' . $originStreet2)); $request->setShipperAddressStreet1($originStreet1); $request->setShipperAddressStreet2($originStreet2); $request->setShipperAddressCity($this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_CITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId)); $request->setShipperAddressStateOrProvinceCode($shipperRegionCode); $request->setShipperAddressPostalCode($this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ZIP, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId)); $request->setShipperAddressCountryCode($this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_COUNTRY_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipmentStoreId)); $request->setRecipientContactPersonName(trim($address->getFirstname() . ' ' . $address->getLastname())); $request->setRecipientContactPersonFirstName($address->getFirstname()); $request->setRecipientContactPersonLastName($address->getLastname()); $request->setRecipientContactCompanyName($address->getCompany()); $request->setRecipientContactPhoneNumber($address->getTelephone()); $request->setRecipientEmail($address->getEmail()); $request->setRecipientAddressStreet(trim($address->getStreetLine(1) . ' ' . $address->getStreetLine(2))); $request->setRecipientAddressStreet1($address->getStreetLine(1)); $request->setRecipientAddressStreet2($address->getStreetLine(2)); $request->setRecipientAddressCity($address->getCity()); $request->setRecipientAddressStateOrProvinceCode($address->getRegionCode()); $request->setRecipientAddressRegionCode($recipientRegionCode); $request->setRecipientAddressPostalCode($address->getPostcode()); $request->setRecipientAddressCountryCode($address->getCountryId()); $request->setShippingMethod($shippingMethod->getMethod()); $request->setPackageWeight($order->getWeight()); $request->setPackages($orderShipment->getPackages()); $request->setBaseCurrencyCode($baseCurrencyCode); $request->setStoreId($shipmentStoreId); return $shipmentCarrier->requestToShipment($request); }
/** * Retrieve tracking url with params * * @param string $key * @param \Magento\Sales\Model\Order|\Magento\Sales\Model\Order\Shipment|\Magento\Sales\Model\Order\Shipment\Track $model * @param string $method Optional - method of a model to get id * @return string */ protected function _getTrackingUrl($key, $model, $method = 'getId') { $urlPart = "{$key}:{$model->{$method}()}:{$model->getProtectCode()}"; $param = array('hash' => $this->_coreData->urlEncode($urlPart)); $storeModel = $this->_storeManager->getStore($model->getStoreId()); return $storeModel->getUrl('shipping/tracking/popup', $param); }