vendor/shopware/core/Content/Product/Aggregate/ProductPrice/ProductPriceCollection.php line 12

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\Aggregate\ProductPrice;
  3. use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  6. use Shopware\Core\Framework\Log\Package;
  7. /**
  8.  * @extends EntityCollection<ProductPriceEntity>
  9.  */
  10. #[Package('inventory')]
  11. class ProductPriceCollection extends EntityCollection
  12. {
  13.     public function getApiAlias(): string
  14.     {
  15.         return 'product_price_collection';
  16.     }
  17.     public function filterByRuleId(string $ruleId): self
  18.     {
  19.         return $this->filter(fn (ProductPriceEntity $price) => $ruleId === $price->getRuleId());
  20.     }
  21.     public function sortByQuantity(): void
  22.     {
  23.         $this->sort(fn (ProductPriceEntity $aProductPriceEntity $b) => $a->getQuantityStart() <=> $b->getQuantityStart());
  24.     }
  25.     public function sortByPrice(Context $context): void
  26.     {
  27.         $this->sort(function (ProductPriceEntity $aProductPriceEntity $b) use ($context) {
  28.             $a $a->getPrice()->first();
  29.             $b $b->getPrice()->first();
  30.             if ($context->getTaxState() === CartPrice::TAX_STATE_GROSS) {
  31.                 return ($a $a->getGross() : 0) <=> ($b $b->getGross() : 0);
  32.             }
  33.             return ($a $a->getNet() : 0) <=> ($b $b->getNet() : 0);
  34.         });
  35.     }
  36.     protected function getExpectedClass(): string
  37.     {
  38.         return ProductPriceEntity::class;
  39.     }
  40. }