vendor/shopware/core/Checkout/Shipping/ShippingMethodCollection.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Shipping;
  3. use Shopware\Core\Checkout\Shipping\Aggregate\ShippingMethodPrice\ShippingMethodPriceCollection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  5. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  6. /**
  7.  * @method void                      add(ShippingMethodEntity $entity)
  8.  * @method void                      set(string $key, ShippingMethodEntity $entity)
  9.  * @method ShippingMethodEntity[]    getIterator()
  10.  * @method ShippingMethodEntity[]    getElements()
  11.  * @method ShippingMethodEntity|null get(string $key)
  12.  * @method ShippingMethodEntity|null first()
  13.  * @method ShippingMethodEntity|null last()
  14.  */
  15. class ShippingMethodCollection extends EntityCollection
  16. {
  17.     public function filterByActiveRules(SalesChannelContext $salesChannelContext): ShippingMethodCollection
  18.     {
  19.         return $this->filter(
  20.             function (ShippingMethodEntity $shippingMethod) use ($salesChannelContext) {
  21.                 return \in_array($shippingMethod->getAvailabilityRuleId(), $salesChannelContext->getRuleIds(), true);
  22.             }
  23.         );
  24.     }
  25.     public function getPriceIds(): array
  26.     {
  27.         $ids = [[]];
  28.         foreach ($this->getIterator() as $element) {
  29.             $ids[] = $element->getPrices()->getIds();
  30.         }
  31.         return array_merge(...$ids);
  32.     }
  33.     public function getPrices(): ShippingMethodPriceCollection
  34.     {
  35.         $prices = [[]];
  36.         foreach ($this->getIterator() as $element) {
  37.             $prices[] = $element->getPrices();
  38.         }
  39.         $prices array_merge(...$prices);
  40.         return new ShippingMethodPriceCollection($prices);
  41.     }
  42.     /**
  43.      * Sorts the selected shipping method first
  44.      * If a different default shipping method is defined, it will be sorted second
  45.      * All other shipping methods keep their respective sorting
  46.      */
  47.     public function sortShippingMethodsByPreference(SalesChannelContext $context): void
  48.     {
  49.         $ids array_merge(
  50.             [$context->getShippingMethod()->getId(), $context->getSalesChannel()->getShippingMethodId()],
  51.             $this->getIds()
  52.         );
  53.         $this->sortByIdArray($ids);
  54.     }
  55.     public function getApiAlias(): string
  56.     {
  57.         return 'shipping_method_collection';
  58.     }
  59.     protected function getExpectedClass(): string
  60.     {
  61.         return ShippingMethodEntity::class;
  62.     }
  63. }