public function testModelToDataObject() { $customerTaxClassId = 100; $shippingPriceExclTax = 1000; $shippingPriceInclTax = 1500; $price = 90.12; $this->storeManagerMock->expects($this->once())->method('getStore')->will($this->returnValue($this->storeMock)); $this->storeMock->expects($this->once())->method('getBaseCurrency')->will($this->returnValue($this->currencyMock)); $this->rateModelMock->expects($this->once())->method('getCarrier')->will($this->returnValue('CARRIER_CODE')); $this->rateModelMock->expects($this->once())->method('getMethod')->will($this->returnValue('METHOD_CODE')); $this->rateModelMock->expects($this->any())->method('getPrice')->will($this->returnValue($price)); $this->currencyMock->expects($this->at(0))->method('convert')->with($price, 'USD')->willReturn(100.12); $this->currencyMock->expects($this->at(1))->method('convert')->with($shippingPriceExclTax, 'USD')->willReturn($shippingPriceExclTax); $this->currencyMock->expects($this->at(2))->method('convert')->with($shippingPriceInclTax, 'USD')->willReturn($shippingPriceInclTax); $this->rateModelMock->expects($this->once())->method('getCarrierTitle')->will($this->returnValue('CARRIER_TITLE')); $this->rateModelMock->expects($this->once())->method('getMethodTitle')->will($this->returnValue('METHOD_TITLE')); $quoteMock = $this->getMock('\\Magento\\Quote\\Model\\Quote', [], [], '', false); $addressMock = $this->getMock('\\Magento\\Quote\\Model\\Quote\\Address', [], [], '', false); $this->rateModelMock->expects($this->exactly(4))->method('getAddress')->willReturn($addressMock); $addressMock->expects($this->exactly(2))->method('getQuote')->willReturn($quoteMock); $quoteMock->expects($this->exactly(2))->method('getCustomerTaxClassId')->willReturn($customerTaxClassId); $this->shippingMethodDataFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setCarrierCode')->with('CARRIER_CODE')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setMethodCode')->with('METHOD_CODE')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setCarrierTitle')->with('CARRIER_TITLE')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setMethodTitle')->with('METHOD_TITLE')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setAmount')->with('100.12')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setBaseAmount')->with('90.12')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setAvailable')->with(true)->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setPriceExclTax')->with($shippingPriceExclTax)->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setPriceInclTax')->with($shippingPriceInclTax)->will($this->returnValue($this->shippingMethodMock)); $this->taxHelper->expects($this->at(0))->method('getShippingPrice')->with($price, false, $addressMock, $customerTaxClassId)->willReturn($shippingPriceExclTax); $this->taxHelper->expects($this->at(1))->method('getShippingPrice')->with($price, true, $addressMock, $customerTaxClassId)->willReturn($shippingPriceInclTax); $this->assertEquals($this->shippingMethodMock, $this->converter->modelToDataObject($this->rateModelMock, 'USD')); }
public function testModelToDataObject() { $this->storeManagerMock->expects($this->once())->method('getStore')->will($this->returnValue($this->storeMock)); $this->storeMock->expects($this->once())->method('getBaseCurrency')->will($this->returnValue($this->currencyMock)); $this->rateModelMock->expects($this->once())->method('getCarrier')->will($this->returnValue('CARRIER_CODE')); $this->rateModelMock->expects($this->once())->method('getMethod')->will($this->returnValue('METHOD_CODE')); $this->rateModelMock->expects($this->any())->method('getPrice')->will($this->returnValue(90.12)); $this->currencyMock->expects($this->once())->method('convert')->with(90.12, 'USD')->will($this->returnValue(100.12)); $this->rateModelMock->expects($this->once())->method('getCarrierTitle')->will($this->returnValue('CARRIER_TITLE')); $this->rateModelMock->expects($this->once())->method('getMethodTitle')->will($this->returnValue('METHOD_TITLE')); $this->shippingMethodDataFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setCarrierCode')->with('CARRIER_CODE')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setMethodCode')->with('METHOD_CODE')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setCarrierTitle')->with('CARRIER_TITLE')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setMethodTitle')->with('METHOD_TITLE')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setAmount')->with('100.12')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setBaseAmount')->with('90.12')->will($this->returnValue($this->shippingMethodMock)); $this->shippingMethodMock->expects($this->once())->method('setAvailable')->with(true)->will($this->returnValue($this->shippingMethodMock)); $this->assertEquals($this->shippingMethodMock, $this->converter->modelToDataObject($this->rateModelMock, 'USD')); }
/** * {@inheritDoc} */ public function getList($cartId) { $output = []; /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); // no methods applicable for empty carts or carts with virtual products if ($quote->isVirtual() || 0 == $quote->getItemsCount()) { return []; } $shippingAddress = $quote->getShippingAddress(); if (!$shippingAddress->getCountryId()) { throw new StateException(__('Shipping address not set.')); } $shippingAddress->collectShippingRates(); $shippingRates = $shippingAddress->getGroupedAllShippingRates(); foreach ($shippingRates as $carrierRates) { foreach ($carrierRates as $rate) { $output[] = $this->converter->modelToDataObject($rate, $quote->getQuoteCurrencyCode()); } } return $output; }
/** * Get list of available shipping methods * @param \Magento\Quote\Model\Quote $quote * @param array $addressData * @return \Magento\Quote\Api\Data\ShippingMethodInterface[] */ private function getShippingMethods(Quote $quote, array $addressData) { $output = []; $shippingAddress = $quote->getShippingAddress(); $shippingAddress->addData($addressData); $shippingAddress->setCollectShippingRates(true); $this->totalsCollector->collectAddressTotals($quote, $shippingAddress); $shippingRates = $shippingAddress->getGroupedAllShippingRates(); foreach ($shippingRates as $carrierRates) { foreach ($carrierRates as $rate) { $output[] = $this->converter->modelToDataObject($rate, $quote->getQuoteCurrencyCode()); } } return $output; }
/** * Get estimated rates * * @param Quote $quote * @param int $country * @param string $postcode * @param int $regionId * @param string $region * @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods. */ protected function getEstimatedRates(\Magento\Quote\Model\Quote $quote, $country, $postcode, $regionId, $region) { $output = []; $shippingAddress = $quote->getShippingAddress(); $shippingAddress->setCountryId($country); $shippingAddress->setPostcode($postcode); $shippingAddress->setRegionId($regionId); $shippingAddress->setRegion($region); $shippingAddress->setCollectShippingRates(true); $shippingAddress->collectTotals(); $shippingRates = $shippingAddress->getGroupedAllShippingRates(); foreach ($shippingRates as $carrierRates) { foreach ($carrierRates as $rate) { $output[] = $this->converter->modelToDataObject($rate, $quote->getQuoteCurrencyCode()); } } return $output; }
public function reqeustShippingRates($carrierCode, $carriergroupId, $addressId = false) { // if (empty($this->_rates)) { // if(!$address->getFreeMethodWeight()) { // $address->setFreeMethodWeight(Mage::getSingleton('checkout/session')->getFreemethodWeight()); // } if (!$this->quote) { $this->quote = $this->checkoutSession->getQuote(); } $address = $this->getAddress(); $rateFound = $address->requestShippingRates(); $address->save(); $rates = $address->getGroupedAllShippingRates(); foreach ($rates as $carrierRates) { foreach ($carrierRates as $rate) { $rateObject = $this->converter->modelToDataObject($rate, $this->quote->getQuoteCurrencyCode()); //mimicking output format from serviceOutputProcessor->process(... (API stuff) $oneRate = ['carrier_code' => $rateObject->getCarrierCode(), 'method_code' => $rateObject->getMethodCode(), 'carrier_title' => $rateObject->getCarrierTitle(), 'method_title' => $rateObject->getMethodTitle(), 'amount' => $rateObject->getAmount(), 'base_amount' => $rateObject->getBaseAmount(), 'available' => $rateObject->getAvailable(), 'error_message' => $rateObject->getErrorMessage(), 'price_excl_tax' => $rateObject->getPriceExclTax(), 'price_incl_tax' => $rateObject->getPriceInclTax()]; $output[] = $oneRate; } } return $output; }