<?php
namespace CioCustomerPermissionGroups\Subscriber;
use CioCustomerPermissionGroups\Event\CustomerAclRolesEvent;
use CioCustomerPermissionGroups\Service\CustomerPermissionService;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomerProfileRolesSubscriber implements EventSubscriberInterface
{
private CustomerPermissionService $checkCustomerPermissionsService;
public function __construct(CustomerPermissionService $checkCustomerPermissionsService)
{
$this->checkCustomerPermissionsService = $checkCustomerPermissionsService;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
CustomerAclRolesEvent::class => 'onCustomerAclRolesEvent',
// StorefrontRenderEvent::class => 'onStorefrontRenderEvent',
ProductPageLoadedEvent::class => 'onProductPageLoadedEvent',
ProductListingResultEvent::class => 'onProductListingResultEvent',
];
}
public function onCustomerAclRolesEvent(CustomerAclRolesEvent $event)
{
// add all in this plugin used customer acl roles
$event->addRoles([
[
'title' => 'PERSONAL_PROFILE_ACCOUNT_EDIT',
'description' => 'Kunde kann Profildaten im Kundenaccount pflegen.'
],
[
'title' => 'PAYMENT_METHOD_ACCOUNT_EDIT',
'description' => 'Kunde kann Zahlungsart im Kundenaccount pflegen.'
],
[
'title' => 'NEWSLETTER_SUBSCRIBE',
'description' => 'Kunde kann Newsletter abonieren oder abbestellen.'
],
[
'title' => 'ALLOW_ADD_TO_CART',
'description' => 'Erlaubt das in den Warenkorb legen von Artikeln'
],
[
'title' => 'ALLOW_ADD_TO_CART_WITH_ZERO_POINTS',
'description' => 'Erlaubt das in den Warenkorb legen von Artikeln mit 0 Punkten'
]
]);
}
public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
{
//dd($this->checkCustomerPermissionsService->check($event->getSalesChannelContext()->getCustomer(), 'perm_allow_create_budget', $event->getContext()));
}
public function onProductPageLoadedEvent(ProductPageLoadedEvent $event)
{
$customer = $event->getSalesChannelContext()->getCustomer();
$context = $event->getSalesChannelContext()->getContext();
$product = $event->getPage()->getProduct();
if ($customer) {
if ($this->checkCustomerPermissionsService->check($customer, 'ALLOW_ADD_TO_CART_WITH_ZERO_POINTS', $context)) {
if ($product->getCalculatedPrice()->getTotalPrice() == 0 || $this->checkCustomerPermissionsService->check($customer, 'ALLOW_ADD_TO_CART', $context)) {
$product->setAvailable(true);
} else {
$product->setAvailable(false);
$product->setMinPurchase(999999);
}
} else if (!$this->checkCustomerPermissionsService->check($customer, 'ALLOW_ADD_TO_CART', $context)) {
$product->setAvailable(false);
$product->setMinPurchase(999999);
}
}
}
public function onProductListingResultEvent(ProductListingResultEvent $event)
{
$customer = $event->getSalesChannelContext()->getCustomer();
$context = $event->getSalesChannelContext()->getContext();
if ($customer) {
if ($this->checkCustomerPermissionsService->check($customer, 'ALLOW_ADD_TO_CART_WITH_ZERO_POINTS', $context)) {
foreach ($event->getResult()->getEntities() as $product) {
/** @var $product ProductEntity */
if ($product->getCalculatedPrice()->getTotalPrice() == 0 || $this->checkCustomerPermissionsService->check($customer, 'ALLOW_ADD_TO_CART', $context)) {
$product->setAvailable(true);
} else {
$product->setAvailable(false);
$product->setMinPurchase(999999);
}
}
} else if (!$this->checkCustomerPermissionsService->check($customer, 'ALLOW_ADD_TO_CART', $context)) {
foreach ($event->getResult()->getEntities() as $product) {
/** @var $product ProductEntity */
$product->setAvailable(false);
$product->setMinPurchase(999999);
}
}
}
}
}