public function testGetOrder() { $orderId = 100000041; $this->shipment->setOrderId($orderId); $entityName = 'shipment'; $order = $this->getMock('Magento\\Sales\\Model\\Order', ['load', 'setHistoryEntityName', '__wakeUp'], [], '', false); $this->shipment->setOrderId($orderId); $order->expects($this->atLeastOnce())->method('setHistoryEntityName')->with($entityName)->will($this->returnSelf()); $this->orderRepository->expects($this->atLeastOnce())->method('get')->will($this->returnValue($order)); $this->assertEquals($order, $this->shipment->getOrder()); }
/** * @param \Magento\Sales\Model\Order\Shipment $shipment * @param RequestInterface $request * @return void * @throws \Magento\Framework\Exception\LocalizedException */ public function create(\Magento\Sales\Model\Order\Shipment $shipment, RequestInterface $request) { $order = $shipment->getOrder(); $carrier = $this->carrierFactory->create($order->getShippingMethod(true)->getCarrierCode()); if (!$carrier->isShippingLabelsAvailable()) { throw new \Magento\Framework\Exception\LocalizedException(__('Shipping labels is not available.')); } $shipment->setPackages($request->getParam('packages')); $response = $this->labelFactory->create()->requestToShipment($shipment); if ($response->hasErrors()) { throw new \Magento\Framework\Exception\LocalizedException(__($response->getErrors())); } if (!$response->hasInfo()) { throw new \Magento\Framework\Exception\LocalizedException(__('Response info is not exist.')); } $labelsContent = []; $trackingNumbers = []; $info = $response->getInfo(); foreach ($info as $inf) { if (!empty($inf['tracking_number']) && !empty($inf['label_content'])) { $labelsContent[] = $inf['label_content']; $trackingNumbers[] = $inf['tracking_number']; } } $outputPdf = $this->combineLabelsPdf($labelsContent); $shipment->setShippingLabel($outputPdf->render()); $carrierCode = $carrier->getCarrierCode(); $carrierTitle = $this->scopeConfig->getValue('carriers/' . $carrierCode . '/title', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $shipment->getStoreId()); if (!empty($trackingNumbers)) { $this->addTrackingNumbersToShipment($shipment, $trackingNumbers, $carrierCode, $carrierTitle); } }
/** * Save shipment and order in one transaction * * @param \Magento\Sales\Model\Order\Shipment $shipment * @return $this */ protected function _saveShipment($shipment) { $shipment->getOrder()->setIsInProcess(true); $transaction = $this->_objectManager->create('Magento\\Framework\\DB\\Transaction'); $transaction->addObject($shipment)->addObject($shipment->getOrder())->save(); return $this; }
/** * 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); }
/** * 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); }
/** * Send email to customer * * @param Shipment $shipment * @param bool $notify * @param string $comment * @return bool */ public function send(Shipment $shipment, $notify = true, $comment = '') { $order = $shipment->getOrder(); $transport = ['order' => $order, 'shipment' => $shipment, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'store' => $order->getStore(), 'formattedShippingAddress' => $this->getFormattedShippingAddress($order), 'formattedBillingAddress' => $this->getFormattedBillingAddress($order)]; $this->eventManager->dispatch('email_shipment_comment_set_template_vars_before', ['sender' => $this, 'transport' => $transport]); $this->templateContainer->setTemplateVars($transport); return $this->checkAndSend($order, $notify); }
/** * Send email to customer * * @param Shipment $shipment * @param bool $notify * @param string $comment * @return bool */ public function send(Shipment $shipment, $notify = true, $comment = '') { $order = $shipment->getOrder(); $this->templateContainer->setTemplateVars(['order' => $order, 'shipment' => $shipment, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'payment_html' => $this->getPaymentHtml($order), 'store' => $order->getStore()]); $result = $this->checkAndSend($order, $notify); if ($result) { $shipment->setEmailSent(true); $this->shipmentResource->saveAttribute($shipment, 'email_sent'); } return $result; }
/** * Send email to customer * * @param Shipment $shipment * @param bool $notify * @param string $comment * @return bool */ public function send(Shipment $shipment, $notify = true, $comment = '') { $order = $shipment->getOrder(); if ($order->getShippingAddress()) { $formattedShippingAddress = $this->addressRenderer->format($order->getShippingAddress(), 'html'); } else { $formattedShippingAddress = ''; } $formattedBillingAddress = $this->addressRenderer->format($order->getBillingAddress(), 'html'); $transport = new \Magento\Framework\Object(['template_vars' => ['order' => $order, 'shipment' => $shipment, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'store' => $order->getStore(), 'formattedShippingAddress' => $formattedShippingAddress, 'formattedBillingAddress' => $formattedBillingAddress]]); $this->eventManager->dispatch('email_shipment_comment_set_template_vars_before', ['sender' => $this, 'transport' => $transport]); $this->templateContainer->setTemplateVars($transport->getTemplateVars()); return $this->checkAndSend($order, $notify); }
/** * Send email to customer * * @param Shipment $shipment * @param bool $notify * @param string $comment * @return bool */ public function send(Shipment $shipment, $notify = true, $comment = '') { $order = $shipment->getOrder(); $this->templateContainer->setTemplateVars(['order' => $order, 'shipment' => $shipment, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'store' => $order->getStore()]); return $this->checkAndSend($order, $notify); }
/** * Sends order shipment email to the customer. * * Email will be sent immediately in two cases: * * - if asynchronous email sending is disabled in global settings * - if $forceSyncMode parameter is set to TRUE * * Otherwise, email will be sent later during running of * corresponding cron job. * * @param Shipment $shipment * @param bool $forceSyncMode * @return bool */ public function send(Shipment $shipment, $forceSyncMode = false) { $shipment->setSendEmail(true); if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) { $order = $shipment->getOrder(); $transport = ['order' => $order, 'shipment' => $shipment, 'comment' => $shipment->getCustomerNoteNotify() ? $shipment->getCustomerNote() : '', 'billing' => $order->getBillingAddress(), 'payment_html' => $this->getPaymentHtml($order), 'store' => $order->getStore(), 'formattedShippingAddress' => $this->getFormattedShippingAddress($order), 'formattedBillingAddress' => $this->getFormattedBillingAddress($order)]; $this->eventManager->dispatch('email_shipment_set_template_vars_before', ['sender' => $this, 'transport' => $transport]); $this->templateContainer->setTemplateVars($transport); if ($this->checkAndSend($order)) { $shipment->setEmailSent(true); $this->shipmentResource->saveAttribute($shipment, ['send_email', 'email_sent']); return true; } } $this->shipmentResource->saveAttribute($shipment, 'send_email'); return false; }