/** * {@inheritdoc} */ public function getDefaultShippingMethod(ShipmentInterface $shipment) { $shippingMethods = $this->shippingMethodRepository->findBy(['enabled' => true]); if (empty($shippingMethods)) { throw new UnresolvedDefaultShippingMethodException(); } return $shippingMethods[0]; }
/** * {@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(); }
/** * {@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; }
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]); }
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]); }