BeforeAddItem
This event belongs to the graphql-storefront module. It will be fired before an item is added to basket and holds the ID of the the user basket, ID of the product and the amount.
BeforeAddItemEventSubscriber
<?php
declare(strict_types=1);
namespace Full\Qualified\Namespace;
use OxidEsales\GraphQL\Storefront\Basket\Event\BeforeAddItem;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DeveloperBeforeAddItemEventSubscriber implements EventSubscriberInterface
{
public function handle(BeforeAddItem $event): BeforeAddItem
{
//get the user basket id from event
$userBasketId = (string) $event->getBasketId();
//get the product id from event
$productId = (string) $event->getProductId();
//get the user basket item amount from event
$amount = (float) $event->getAmount();
//do something
return $event;
}
public static function getSubscribedEvents()
{
return [
'OxidEsales\GraphQL\Storefront\Basket\Event\BeforeAddItem' => 'handle'
];
}
}
Important
The code above is only an example. In case you need to handle the BeforeAddItem
event,
please adapt to your needs.
services.yaml
services:
_defaults:
public: false
autowire: true
Full\Qualified\Namespace\DeveloperBeforeAdItemEventSubscriber:
class: Full\Qualified\Namespace\DeveloperBeforeAddItemEventSubscriber
tags: ['kernel.event_subscriber']