src/Core/Content/Product/ProductMaxPurchaseCalculator.php line 28

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  4. use Shopware\Core\Framework\Log\Package;
  5. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. #[Package('inventory')]
  9. class ProductMaxPurchaseCalculator extends AbstractProductMaxPurchaseCalculator
  10. {
  11.     /**
  12.      * @internal
  13.      */
  14.     public function __construct(private readonly SystemConfigService $systemConfigService)
  15.     {
  16.     }
  17.     public function getDecorated(): AbstractProductMaxPurchaseCalculator
  18.     {
  19.         throw new DecorationPatternException(self::class);
  20.     }
  21.     public function calculate(Entity $productSalesChannelContext $context): int
  22.     {
  23.         $fallback $this->systemConfigService->getInt(
  24.             'core.cart.maxQuantity',
  25.             $context->getSalesChannel()->getId()
  26.         );
  27.         $max $product->get('maxPurchase') ?? $fallback;
  28.         if ($product->get('isCloseout') && $product->get('availableStock') < $max) {
  29.             $max = (int) $product->get('availableStock');
  30.         }
  31.         $steps $product->get('purchaseSteps') ?? 1;
  32.         $min $product->get('minPurchase') ?? 1;
  33.         // the amount of times the purchase step is fitting in between min and max added to the minimum
  34.         $max \floor(($max $min) / $steps) * $steps $min;
  35.         return (int) \max($max0);
  36.     }
  37. }