custom/static-plugins/SamsonCustomer/src/Subscriber/SalesChannelProductSubscriber.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Subscriber;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2022
  11.  *
  12.  ***/
  13. use Shopware\Core\Content\Product\SalesChannel\Price\AbstractProductPriceCalculator;
  14. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  15. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Shopware\Core\System\SystemConfig\SystemConfigService;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class SalesChannelProductSubscriber implements EventSubscriberInterface
  20. {
  21.     private SystemConfigService $systemConfigService;
  22.     private AbstractProductPriceCalculator $calculator;
  23.     public function __construct(
  24.         SystemConfigService $systemConfigService,
  25.         AbstractProductPriceCalculator $calculator
  26.     ) {
  27.         $this->systemConfigService $systemConfigService;
  28.         $this->calculator $calculator;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             'sales_channel.product.loaded' => ['loaded', -1],
  34.         ];
  35.     }
  36.     public function loaded(SalesChannelEntityLoadedEvent $event): void
  37.     {
  38.         $this->calculator->calculate($event->getEntities(), $event->getSalesChannelContext());
  39.         /** @var SalesChannelProductEntity $product */
  40.         foreach ($event->getEntities() as $product) {
  41.             $product->setCalculatedMaxPurchase(
  42.                 $this->calculateMaxPurchase($product$event->getSalesChannelContext()->getSalesChannel()->getId())
  43.             );
  44.             $this->markAsNew($event->getSalesChannelContext(), $product);
  45.         }
  46.     }
  47.     private function calculateMaxPurchase(SalesChannelProductEntity $productstring $salesChannelId): int
  48.     {
  49.         $fallback $this->systemConfigService->getInt('core.cart.maxQuantity'$salesChannelId);
  50.         $max $product->getMaxPurchase() ?? $fallback;
  51.         if ($product->getIsCloseout() && $product->getAvailableStock() < $max) {
  52.             $max = (int) $product->getAvailableStock();
  53.         }
  54.         $steps $product->getPurchaseSteps() ?? 1;
  55.         $min $product->getMinPurchase() ?? 1;
  56.         // the amount of times the purchase step is fitting in between min and max added to the minimum
  57.         $max \floor(($max $min) / $steps) * $steps $min;
  58.         return (int) \max($max0);
  59.     }
  60.     private function markAsNew(SalesChannelContext $contextSalesChannelProductEntity $product): void
  61.     {
  62.         $markAsNewDayRange $this->systemConfigService->get('core.listing.markAsNew'$context->getSalesChannel()->getId());
  63.         $now = new \DateTime();
  64.         /* @var SalesChannelProductEntity $product */
  65.         $product->setIsNew(
  66.             $product->getReleaseDate() instanceof \DateTimeInterface
  67.             && $product->getReleaseDate()->diff($now)->days <= $markAsNewDayRange
  68.         );
  69.     }
  70. }