custom/plugins/Neon6Configurator/src/Cart/Checkout/ConfiguratorProductMaxPurchaseCalculator.php line 55

Open in your IDE?
  1. <?php
  2. namespace Neon\Configurator\Cart\Checkout;
  3. use Neon\Configurator\Core\Content\Configuration\ConfigurationEntity;
  4. use Neon\Configurator\Services\ConfigurationLoader;
  5. use Shopware\Core\Content\Product\ProductMaxPurchaseCalculator;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. class ConfiguratorProductMaxPurchaseCalculator extends ProductMaxPurchaseCalculator
  10. {
  11.     const MAX_VALUE_FALLBACK 2147483647;
  12.     private SystemConfigService $systemConfigService;
  13.     private ConfigurationLoader $configurationLoader;
  14.     public function __construct(SystemConfigService $systemConfigServiceConfigurationLoader $configurationLoader)
  15.     {
  16.         parent::__construct($systemConfigService);
  17.         $this->systemConfigService $systemConfigService;
  18.         $this->configurationLoader $configurationLoader;
  19.     }
  20.     public function calculate(Entity $productSalesChannelContext $context): int
  21.     {
  22.         $fallback $this->systemConfigService->getInt(
  23.             'core.cart.maxQuantity',
  24.             $context->getSalesChannel()->getId()
  25.         );
  26.         $fallback $this->setFallbackToMaxMysqlIntValueIfConfigProductWithCalcualtedQuantity($product$context$fallback);
  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.     /**
  38.      * @param SalesChannelContext $context
  39.      * @param int $fallback
  40.      * @return
  41.      */
  42.     private function setFallbackToMaxMysqlIntValueIfConfigProductWithCalcualtedQuantity($productSalesChannelContext $context$fallback)
  43.     {
  44.         /** @var ConfigurationEntity $configuration */
  45.         $configuration $this->configurationLoader->loadWithoutAssociations($product$context);
  46.         if ($configuration != null && ($configuration->getQuantityMode() == 'field' || $configuration->getQuantityMode() == 'calculation')) {
  47.             $fallback self::MAX_VALUE_FALLBACK;
  48.         }
  49.         return $fallback;
  50.     }
  51. }