vendor/shopware/core/Checkout/Cart/Delivery/Struct/DeliveryCollection.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\Delivery\Struct;
  3. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  4. use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection;
  5. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressCollection;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Core\Framework\Struct\Collection;
  8. /**
  9.  * @extends Collection<Delivery>
  10.  */
  11. #[Package('checkout')]
  12. class DeliveryCollection extends Collection
  13. {
  14.     /**
  15.      * Sorts the delivery collection by earliest delivery date
  16.      */
  17.     public function sortDeliveries(): self
  18.     {
  19.         $this->sort(function (Delivery $aDelivery $b) {
  20.             if ($a->getLocation() !== $b->getLocation()) {
  21.                 return -1;
  22.             }
  23.             return $a->getDeliveryDate()->getEarliest() > $b->getDeliveryDate()->getEarliest();
  24.         });
  25.         return $this;
  26.     }
  27.     public function getDelivery(DeliveryDate $deliveryDateShippingLocation $location): ?Delivery
  28.     {
  29.         foreach ($this->getIterator() as $delivery) {
  30.             if ($delivery->getDeliveryDate()->getEarliest()->format('Y-m-d') !== $deliveryDate->getEarliest()->format('Y-m-d')) {
  31.                 continue;
  32.             }
  33.             if ($delivery->getDeliveryDate()->getLatest()->format('Y-m-d') !== $deliveryDate->getLatest()->format('Y-m-d')) {
  34.                 continue;
  35.             }
  36.             if ($delivery->getLocation() !== $location) {
  37.                 continue;
  38.             }
  39.             return $delivery;
  40.         }
  41.         return null;
  42.     }
  43.     public function contains(LineItem $item): bool
  44.     {
  45.         foreach ($this->getIterator() as $delivery) {
  46.             if ($delivery->getPositions()->has($item->getId())) {
  47.                 return true;
  48.             }
  49.         }
  50.         return false;
  51.     }
  52.     public function getShippingCosts(): PriceCollection
  53.     {
  54.         return new PriceCollection(
  55.             $this->map(function (Delivery $delivery) {
  56.                 return $delivery->getShippingCosts();
  57.             })
  58.         );
  59.     }
  60.     public function getAddresses(): CustomerAddressCollection
  61.     {
  62.         $addresses = new CustomerAddressCollection();
  63.         foreach ($this->getIterator() as $delivery) {
  64.             $address $delivery->getLocation()->getAddress();
  65.             if ($address !== null) {
  66.                 $addresses->add($address);
  67.             }
  68.         }
  69.         return $addresses;
  70.     }
  71.     public function getApiAlias(): string
  72.     {
  73.         return 'cart_delivery_collection';
  74.     }
  75.     protected function getExpectedClass(): ?string
  76.     {
  77.         return Delivery::class;
  78.     }
  79. }