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

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