custom/plugins/CioCustomerPermissionGroups/src/Subscriber/CustomerProfileRolesSubscriber.php line 86

Open in your IDE?
  1. <?php
  2. namespace CioCustomerPermissionGroups\Subscriber;
  3. use CioCustomerPermissionGroups\Event\CustomerAclRolesEvent;
  4. use CioCustomerPermissionGroups\Service\CustomerPermissionService;
  5. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  6. use Shopware\Core\Content\Product\ProductEntity;
  7. use Shopware\Storefront\Event\StorefrontRenderEvent;
  8. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class CustomerProfileRolesSubscriber implements EventSubscriberInterface
  11. {
  12.     private CustomerPermissionService $checkCustomerPermissionsService;
  13.     public function __construct(CustomerPermissionService $checkCustomerPermissionsService)
  14.     {
  15.         $this->checkCustomerPermissionsService $checkCustomerPermissionsService;
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  20.         return [
  21.             CustomerAclRolesEvent::class => 'onCustomerAclRolesEvent',
  22.             // StorefrontRenderEvent::class => 'onStorefrontRenderEvent',
  23.             ProductPageLoadedEvent::class => 'onProductPageLoadedEvent',
  24.             ProductListingResultEvent::class => 'onProductListingResultEvent',
  25.         ];
  26.     }
  27.     public function onCustomerAclRolesEvent(CustomerAclRolesEvent $event)
  28.     {
  29.         // add all in this plugin used customer acl roles
  30.         $event->addRoles([
  31.             [
  32.                 'title' => 'PERSONAL_PROFILE_ACCOUNT_EDIT',
  33.                 'description' => 'Kunde kann Profildaten im Kundenaccount pflegen.'
  34.             ],
  35.             [
  36.                 'title' => 'PAYMENT_METHOD_ACCOUNT_EDIT',
  37.                 'description' => 'Kunde kann Zahlungsart im Kundenaccount pflegen.'
  38.             ],
  39.             [
  40.                 'title' => 'NEWSLETTER_SUBSCRIBE',
  41.                 'description' => 'Kunde kann Newsletter abonieren oder abbestellen.'
  42.             ],
  43.             [
  44.                 'title' => 'ALLOW_ADD_TO_CART',
  45.                 'description' => 'Erlaubt das in den Warenkorb legen von Artikeln'
  46.             ],
  47.             [
  48.                 'title' => 'ALLOW_ADD_TO_CART_WITH_ZERO_POINTS',
  49.                 'description' => 'Erlaubt das in den Warenkorb legen von Artikeln mit 0 Punkten'
  50.             ]
  51.         ]);
  52.     }
  53.     public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
  54.     {
  55.         //dd($this->checkCustomerPermissionsService->check($event->getSalesChannelContext()->getCustomer(), 'perm_allow_create_budget', $event->getContext()));
  56.     }
  57.     public function onProductPageLoadedEvent(ProductPageLoadedEvent $event)
  58.     {
  59.         $customer $event->getSalesChannelContext()->getCustomer();
  60.         $context $event->getSalesChannelContext()->getContext();
  61.         $product $event->getPage()->getProduct();
  62.         if ($customer) {
  63.             if ($this->checkCustomerPermissionsService->check($customer'ALLOW_ADD_TO_CART_WITH_ZERO_POINTS'$context)) {
  64.                 if ($product->getCalculatedPrice()->getTotalPrice() == || $this->checkCustomerPermissionsService->check($customer'ALLOW_ADD_TO_CART'$context)) {
  65.                     $product->setAvailable(true);
  66.                 } else {
  67.                     $product->setAvailable(false);
  68.                     $product->setMinPurchase(999999);
  69.                 }
  70.             } else if (!$this->checkCustomerPermissionsService->check($customer'ALLOW_ADD_TO_CART'$context)) {
  71.                 $product->setAvailable(false);
  72.                 $product->setMinPurchase(999999);
  73.             }
  74.         }
  75.     }
  76.     public function onProductListingResultEvent(ProductListingResultEvent $event)
  77.     {
  78.         $customer $event->getSalesChannelContext()->getCustomer();
  79.         $context $event->getSalesChannelContext()->getContext();
  80.         if ($customer) {
  81.             if ($this->checkCustomerPermissionsService->check($customer'ALLOW_ADD_TO_CART_WITH_ZERO_POINTS'$context)) {
  82.                 foreach ($event->getResult()->getEntities() as $product) {
  83.                     /** @var $product ProductEntity */
  84.                     if ($product->getCalculatedPrice()->getTotalPrice() == || $this->checkCustomerPermissionsService->check($customer'ALLOW_ADD_TO_CART'$context)) {
  85.                         $product->setAvailable(true);
  86.                     } else {
  87.                         $product->setAvailable(false);
  88.                         $product->setMinPurchase(999999);
  89.                     }
  90.                 }
  91.             } else if (!$this->checkCustomerPermissionsService->check($customer'ALLOW_ADD_TO_CART'$context)) {
  92.                 foreach ($event->getResult()->getEntities() as $product) {
  93.                     /** @var $product ProductEntity */
  94.                     $product->setAvailable(false);
  95.                     $product->setMinPurchase(999999);
  96.                 }
  97.             }
  98.         }
  99.     }
  100. }