custom/plugins/b2bsellerscore/src/Components/Product/Subscriber/ProductAlreadyBoughtSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace B2bSellersCore\Components\Product\Subscriber;
  3. use B2bSellersCore\Components\B2bPlatform\Traits\B2bContextTrait;
  4. use B2bSellersCore\Components\Employee\Exception\InsufficientEmployeePermissionException;
  5. use B2bSellersCore\Components\Employee\Permission\EmployeePermissionTrait;
  6. use B2bSellersCore\Components\Employee\Permission\Permissions;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  11. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class ProductAlreadyBoughtSubscriber implements EventSubscriberInterface
  14. {
  15.     use EmployeePermissionTrait;
  16.     use B2bContextTrait;
  17.     private EntityRepositoryInterface $orderRepository;
  18.     public function __construct(
  19.         EntityRepositoryInterface $orderRepository
  20.     )
  21.     {
  22.         $this->orderRepository $orderRepository;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             ProductPageLoadedEvent::class => 'onProductsLoaded'
  28.         ];
  29.     }
  30.     public function onProductsLoaded(ProductPageLoadedEvent $event)
  31.     {
  32.         if (!$event->getSalesChannelContext()->getCustomer()
  33.             || !$this->hasB2bPlatformContext($event->getSalesChannelContext())
  34.             || !$this->getEmployee($event->getSalesChannelContext())) {
  35.             return;
  36.         }
  37.         $productId $event->getPage()->getProduct()->getId();
  38.         $customerId $event->getSalesChannelContext()->getCustomer()->getId();
  39.         $criteria = new Criteria();
  40.         $criteria->addSorting(new FieldSorting('createdAt''DESC'));
  41.         $criteria->addAssociation('lineItems');
  42.         $criteria->addAssociation('lineItems.product');
  43.         $criteria->addAssociation('lineItems.product.options');
  44.         $criteria->addAssociation('lineItems.product.properties');
  45.         $criteria->addFilter(new EqualsFilter('orderCustomer.customerId'$customerId));
  46.         $criteria->addFilter(new EqualsFilter('lineItems.productId'$productId));
  47.         try {
  48.             $this->checkB2bPlatformPermission([Permissions::EMPLOYEE_PERMISSION_VIEW_ALL_ORDERS], $event->getSalesChannelContext());
  49.         } catch (InsufficientEmployeePermissionException $e) {
  50.             $employee $this->getEmployee($event->getSalesChannelContext());
  51.             if (!$employee) {
  52.                 return;
  53.             }
  54.             $criteria->addFilter(new EqualsFilter(
  55.                 'order.customFields.b2b_order_customer_employee_id',
  56.                 $employee->getId()
  57.             ));
  58.         }
  59.         $order $this->orderRepository->search($criteria$event->getContext())->first();
  60.         if ($order) {
  61.             foreach ($order->getLineItems() as $lineItem) {
  62.                 if ($lineItem->productId === $productId) {
  63.                     $event->getPage()->addExtension('orderProductAlreadyBought'$lineItem);
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }