/** * @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); } }
/** * @covers \Magento\Shipping\Model\Shipping\LabelGenerator * @param array $info * @dataProvider labelInfoDataProvider */ public function testAddTrackingNumbersToShipment(array $info) { $order = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->getMock(); $order->expects(static::once())->method('getShippingMethod')->with(true)->willReturn($this->getShippingMethodMock()); /** * @var $shipmentMock \Magento\Sales\Model\Order\Shipment|\PHPUnit_Framework_MockObject_MockObject */ $shipmentMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Shipment')->disableOriginalConstructor()->getMock(); $shipmentMock->expects(static::once())->method('getOrder')->willReturn($order); $this->carrierFactory->expects(static::once())->method('create')->with(self::CARRIER_CODE)->willReturn($this->getCarrierMock()); $labelsMock = $this->getMockBuilder('\\Magento\\Shipping\\Model\\Shipping\\Labels')->disableOriginalConstructor()->getMock(); $labelsMock->expects(static::once())->method('requestToShipment')->with($shipmentMock)->willReturn($this->getResponseMock($info)); $this->labelsFactory->expects(static::once())->method('create')->willReturn($labelsMock); $this->filesystem->expects(static::once())->method('getDirectoryWrite')->willReturn($this->getMock('Magento\\Framework\\Filesystem\\Directory\\WriteInterface')); $this->scopeConfig->expects(static::once())->method('getValue')->with('carriers/' . self::CARRIER_CODE . '/title', ScopeInterface::SCOPE_STORE, null)->willReturn(self::CARRIER_TITLE); $this->labelsFactory->expects(static::once())->method('create')->willReturn($labelsMock); $trackMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Shipment\\Track')->setMethods(['setNumber', 'setCarrierCode', 'setTitle'])->disableOriginalConstructor()->getMock(); $i = 0; $trackingNumbers = is_array($info['tracking_number']) ? $info['tracking_number'] : [$info['tracking_number']]; foreach ($trackingNumbers as $trackingNumber) { $trackMock->expects(static::at($i++))->method('setNumber')->with($trackingNumber)->willReturnSelf(); $trackMock->expects(static::at($i++))->method('setCarrierCode')->with(self::CARRIER_CODE)->willReturnSelf(); $trackMock->expects(static::at($i++))->method('setTitle')->with(self::CARRIER_TITLE)->willReturnSelf(); } $this->trackFactory->expects(static::any())->method('create')->willReturn($trackMock); /** * @var $requestMock \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ $requestMock = $this->getMock('Magento\\Framework\\App\\RequestInterface'); $this->labelGenerator->create($shipmentMock, $requestMock); }
/** * @param string $code * @return \Magento\Framework\Phrase|string|bool */ public function getCarrierTitle($code) { $carrier = $this->_carrierFactory->create($code); if ($carrier) { return $carrier->getConfigData('title'); } else { return __('Custom Value'); } return false; }
/** * Retrieve all system carriers * * @param mixed $store * @return array */ public function getAllCarriers($store = null) { $carriers = []; $config = $this->_scopeConfig->getValue('carriers', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store); foreach (array_keys($config) as $carrierCode) { $model = $this->_carrierFactory->create($carrierCode, $store); if ($model) { $carriers[$carrierCode] = $model; } } return $carriers; }
/** * Retrieve detail for shipment track * * @return \Magento\Framework\Phrase|string */ public function getNumberDetail() { $carrierInstance = $this->_carrierFactory->create($this->getCarrierCode()); if (!$carrierInstance) { $custom = []; $custom['title'] = $this->getTitle(); $custom['number'] = $this->getTrackNumber(); return $custom; } else { $carrierInstance->setStore($this->getStore()); } $trackingInfo = $carrierInstance->getTrackingInfo($this->getNumber()); if (!$trackingInfo) { return __('No detail for number "%1"', $this->getNumber()); } return $trackingInfo; }
/** * Return content types of package * * @return array */ public function getContentTypes() { $order = $this->getShipment()->getOrder(); $storeId = $this->getShipment()->getStoreId(); $address = $order->getShippingAddress(); $carrier = $this->_carrierFactory->create($order->getShippingMethod(true)->getCarrierCode()); $countryShipper = $this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId); if ($carrier) { $params = new \Magento\Framework\DataObject(['method' => $order->getShippingMethod(true)->getMethod(), 'country_shipper' => $countryShipper, 'country_recipient' => $address->getCountryId()]); return $carrier->getContentTypes($params); } return []; }
/** * Collect rates of given carrier * * @param string $carrierCode * @param \Magento\Sales\Model\Quote\Address\RateRequest $request * @return $this */ public function collectCarrierRates($carrierCode, $request) { /* @var $carrier \Magento\Shipping\Model\Carrier\AbstractCarrier */ $carrier = $this->_carrierFactory->createIfActive($carrierCode, $request->getStoreId()); if (!$carrier) { return $this; } $carrier->setActiveFlag($this->_availabilityConfigField); $result = $carrier->checkAvailableShipCountries($request); if (false !== $result && !$result instanceof \Magento\Sales\Model\Quote\Address\RateResult\Error) { $result = $carrier->proccessAdditionalValidation($request); } /* * Result will be false if the admin set not to show the shipping module * if the delivery country is not within specific countries */ if (false !== $result) { if (!$result instanceof \Magento\Sales\Model\Quote\Address\RateResult\Error) { if ($carrier->getConfigData('shipment_requesttype')) { $packages = $this->composePackagesForCarrier($carrier, $request); if (!empty($packages)) { $sumResults = array(); foreach ($packages as $weight => $packageCount) { $request->setPackageWeight($weight); $result = $carrier->collectRates($request); if (!$result) { return $this; } else { $result->updateRatePrice($packageCount); } $sumResults[] = $result; } if (!empty($sumResults) && count($sumResults) > 1) { $result = array(); foreach ($sumResults as $res) { if (empty($result)) { $result = $res; continue; } foreach ($res->getAllRates() as $method) { foreach ($result->getAllRates() as $resultMethod) { if ($method->getMethod() == $resultMethod->getMethod()) { $resultMethod->setPrice($method->getPrice() + $resultMethod->getPrice()); continue; } } } } } } else { $result = $carrier->collectRates($request); } } else { $result = $carrier->collectRates($request); } if (!$result) { return $this; } } if ($carrier->getConfigData('showmethod') == 0 && $result->getError()) { return $this; } // sort rates by price if (method_exists($result, 'sortRatesByPrice') && is_callable([$result, 'sortRatesByPrice'])) { $result->sortRatesByPrice(); } $this->getResult()->append($result); } return $this; }
/** * Check is carrier has functionality of creation shipping labels * * @return bool */ public function canCreateShippingLabel() { $shippingCarrier = $this->_carrierFactory->create($this->getOrder()->getShippingMethod(true)->getCarrierCode()); return $shippingCarrier && $shippingCarrier->isShippingLabelsAvailable(); }