/** * {@inheritdoc} */ public function getDefaultShippingMethod(ShipmentInterface $shipment) { $shippingMethods = $this->shippingMethodRepository->findBy(['enabled' => true]); if (empty($shippingMethods)) { throw new UnresolvedDefaultShippingMethodException(); } return $shippingMethods[0]; }
/** * @Transform /^"([^"]+)" shipping method$/ * @Transform /^shipping method "([^"]+)"$/ * @Transform :shippingMethod */ public function getShippingMethodByName($shippingMethodName) { $shippingMethod = $this->shippingMethodRepository->findOneByName($shippingMethodName); if (null === $shippingMethod) { throw new \Exception('Shipping method with name "' . $shippingMethodName . '" does not exist'); } return $shippingMethod; }
function it_throws_exception_if_there_is_no_enabled_shipping_methods_for_channel(ChannelInterface $channel, OrderInterface $order, ShipmentInterface $shipment, ShippingMethodInterface $firstShippingMethod, ShippingMethodInterface $secondShippingMethod, ShippingMethodInterface $thirdShippingMethod, ShippingMethodRepositoryInterface $shippingMethodRepository) { $shippingMethodRepository->findBy(['enabled' => true])->willReturn([$firstShippingMethod, $secondShippingMethod, $thirdShippingMethod]); $shipment->getOrder()->willReturn($order); $order->getChannel()->willReturn($channel); $channel->hasShippingMethod($firstShippingMethod)->willReturn(false); $channel->hasShippingMethod($secondShippingMethod)->willReturn(false); $channel->hasShippingMethod($thirdShippingMethod)->willReturn(false); $this->shouldThrow(UnresolvedDefaultShippingMethodException::class)->during('getDefaultShippingMethod', [$shipment]); }
function it_returns_shipping_methods_matched_for_shipment_order_shipping_address_and_order_channel(AddressInterface $address, ChannelInterface $channel, OrderInterface $order, ShipmentInterface $shipment, ShippingMethodInterface $firstShippingMethod, ShippingMethodInterface $secondShippingMethod, ShippingMethodInterface $thirdShippingMethod, ShippingMethodRepositoryInterface $shippingMethodRepository, ZoneInterface $firstZone, ZoneInterface $secondZone, ZoneMatcherInterface $zoneMatcher) { $shipment->getOrder()->willReturn($order); $order->getShippingAddress()->willReturn($address); $order->getChannel()->willReturn($channel); $zoneMatcher->matchAll($address)->willReturn([$firstZone, $secondZone]); $firstZone->getId()->willReturn(1); $secondZone->getId()->willReturn(4); $shippingMethodRepository->findBy(['enabled' => true, 'zone' => [1, 4]])->willReturn([$firstShippingMethod, $secondShippingMethod, $thirdShippingMethod]); $channel->hasShippingMethod($firstShippingMethod)->willReturn(true); $channel->hasShippingMethod($secondShippingMethod)->willReturn(true); $channel->hasShippingMethod($thirdShippingMethod)->willReturn(false); $this->getSupportedMethods($shipment)->shouldReturn([$firstShippingMethod, $secondShippingMethod]); }
/** * {@inheritdoc} */ public function getDefaultShippingMethod(ShipmentInterface $shipment) { Assert::isInstanceOf($shipment, CoreShipmentInterface::class); $shippingMethods = $this->shippingMethodRepository->findBy(['enabled' => true]); if (empty($shippingMethods)) { throw new UnresolvedDefaultShippingMethodException(); } /** @var ChannelInterface $channel */ $channel = $shipment->getOrder()->getChannel(); foreach ($shippingMethods as $shippingMethod) { if ($channel->hasShippingMethod($shippingMethod)) { return $shippingMethod; } } throw new UnresolvedDefaultShippingMethodException(); }
/** * @param string $name * @param string|null $code * @param int|null $position * @param ZoneInterface|null $zone * @param string $locale * @param array $configuration * @param string $calculator * @param bool $enabled * @param bool $addForCurrentChannel * @param array $channels * * @return ShippingMethodInterface */ private function createShippingMethod($name, $code = null, $position = null, ZoneInterface $zone = null, $locale = 'en', array $configuration = [], $calculator = DefaultCalculators::FLAT_RATE, $enabled = true, $addForCurrentChannel = true, array $channels = []) { $channel = $this->sharedStorage->get('channel'); if (null === $zone) { $zone = $this->sharedStorage->get('zone'); } if (null === $code) { $code = $this->generateCodeFromNameAndZone($name, $zone->getCode()); } if (empty($configuration)) { $configuration = $this->getConfigurationByChannels([$channel]); } /** @var ShippingMethodInterface $shippingMethod */ $shippingMethod = $this->shippingMethodFactory->createNew(); $shippingMethod->setCode($code); $shippingMethod->setName($name); $shippingMethod->setPosition($position); $shippingMethod->setCurrentLocale($locale); $shippingMethod->setConfiguration($configuration); $shippingMethod->setCalculator($calculator); $shippingMethod->setZone($zone); $shippingMethod->setEnabled($enabled); if ($addForCurrentChannel && $channel) { $shippingMethod->addChannel($channel); } if (!$addForCurrentChannel) { foreach ($channels as $channel) { $shippingMethod->addChannel($channel); } } $this->shippingMethodRepository->add($shippingMethod); $this->sharedStorage->set('shipping_method', $shippingMethod); return $shippingMethod; }
/** * {@inheritdoc} */ public function getSupportedMethods(ShippingSubjectInterface $subject) { $zones = $this->getZonesIdsForAddress($subject->getOrder()); if (empty($zones)) { return []; } /** @var ChannelInterface $channel */ $channel = $subject->getOrder()->getChannel(); $methods = []; foreach ($this->shippingMethodRepository->findBy(['enabled' => true, 'zone' => $zones]) as $method) { if ($channel->hasShippingMethod($method)) { $methods[] = $method; } } return $methods; }
/** * @param string $name * @param string|null $code * @param int|null $position * @param ZoneInterface|null $zone * @param string $locale * @param array $configuration * @param string $calculator * @param bool $enabled * @param bool $addForCurrentChannel */ private function createShippingMethod($name, $code = null, $position = null, ZoneInterface $zone = null, $locale = 'en', $configuration = ['amount' => 0], $calculator = DefaultCalculators::FLAT_RATE, $enabled = true, $addForCurrentChannel = true) { if (null === $zone) { $zone = $this->sharedStorage->get('zone'); } if (null === $code) { $code = $this->generateCodeFromNameAndZone($name, $zone->getCode()); } /** @var ShippingMethodInterface $shippingMethod */ $shippingMethod = $this->shippingMethodFactory->createNew(); $shippingMethod->setCode($code); $shippingMethod->setName($name); $shippingMethod->setPosition($position); $shippingMethod->setCurrentLocale($locale); $shippingMethod->setConfiguration($configuration); $shippingMethod->setCalculator($calculator); $shippingMethod->setZone($zone); $shippingMethod->setEnabled($enabled); if ($addForCurrentChannel && $this->sharedStorage->has('channel')) { $channel = $this->sharedStorage->get('channel'); $channel->addShippingMethod($shippingMethod); } $this->shippingMethodRepository->add($shippingMethod); $this->sharedStorage->set('shipping_method', $shippingMethod); }
/** * @param string $name * @param ZoneInterface|null $zone * @param string $locale * @param array $configuration * @param string $calculator */ private function createShippingMethod($name, ZoneInterface $zone = null, $locale = 'en', $configuration = ['amount' => 0], $calculator = DefaultCalculators::FLAT_RATE) { if (null === $zone) { $zone = $this->sharedStorage->get('zone'); } /** @var ShippingMethodInterface $shippingMethod */ $shippingMethod = $this->shippingMethodFactory->createNew(); $shippingMethod->setCode($this->generateCodeFromNameAndZone($name, $zone->getCode())); $shippingMethod->setName($name); $shippingMethod->setCurrentLocale($locale); $shippingMethod->setConfiguration($configuration); $shippingMethod->setCalculator($calculator); $shippingMethod->setZone($zone); $this->shippingMethodRepository->add($shippingMethod); }
/** * @Transform /^"([^"]+)" shipping method$/ * @Transform /^shipping method "([^"]+)"$/ * @Transform :shippingMethod */ public function getShippingMethodByName($shippingMethodName) { $shippingMethods = $this->shippingMethodRepository->findByName($shippingMethodName, 'en_US'); Assert::eq(1, count($shippingMethods), sprintf('%d shipping methods have been found with name "%s".', count($shippingMethods), $shippingMethodName)); return $shippingMethods[0]; }
function it_throws_exception_if_there_is_no_enabled_shipping_methods(ShippingMethodRepositoryInterface $shippingMethodRepository, ShipmentInterface $shipment) { $shippingMethodRepository->findBy(['enabled' => true])->willReturn([]); $this->shouldThrow(UnresolvedDefaultShippingMethodException::class)->during('getDefaultShippingMethod', [$shipment]); }
/** * @Transform /^"([^"]+)" shipping method$/ * @Transform /^shipping method "([^"]+)"$/ * @Transform :shippingMethod */ public function getShippingMethodByName($shippingMethodName) { $shippingMethod = $this->shippingMethodRepository->findOneByName($shippingMethodName); Assert::notNull($shippingMethod, sprintf('Shipping method with name "%s" does not exist', $shippingMethodName)); return $shippingMethod; }