Add or remove subscription intervals

Subscription intervals can be added and removed by using the DeliveryIntervalCollectorEvent.

Adding or removing intervals

Subscription intervals can be added or removed via an event subscriber that listens to the DeliveryIntervalCollectorEvent

// <plugin root>/src/Subscriber/DeliveryIntervalSubscriber.php

namespace B2bSellersExample\Subscriber;

use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use B2bProductSubscription\Components\ProductSubscription\Events\DeliveryIntervalCollectorEvent;
use Symfony\Contracts\Translation\TranslatorInterface;

class DeliveryIntervalSubscriber implements EventSubscriberInterface
{
    public function __construct(
       private readonly TranslatorInterface $translator
    )
    {}
    
    public static function getSubscribedEvents(): array
    {
        return [
            DeliveryIntervalCollectorEvent::class=> 'onChangeDeliveryIntervals'
        ];
    }

    public function onChangeDeliveryIntervals(DeliveryIntervalCollectorEvent $event)
    {
        $collection = $event->getCollection();
        
        // removing the weekly interval option
        $collection->remove('weekly');

        // Adding your custom interval
        $key = 'custom';
        
        $collection->set($key, new DeliveryIntervalStruct(
            $key,
            $this->translator->trans('b2bProductSubscription.interval.' . $key),
            'b2bProductSubscription.interval.' . $key
        ));
    }
}

Providing a processor for custom intervals

If you add a new subscription interval, a corresponding processor for calculating the next delivery/order date must also be available.

The processor must implement the IntervalProcessorInterface and must be registered as a service via the b2b_product_subscription.interval.processor tag.

// <plugin root>/src/ProductSubscription/DeliveryInterval/Processor/CustomIntervalProcessor .php

namespace B2bSellersExample\ProductSubscription\DeliveryInterval\Processor;

class CustomIntervalProcessor implements IntervalProcessorInterface
{
    public const DELIVERY_KEY = 'custom';

    public function isSupported(string $key)
    {
        return self::DELIVERY_KEY === $key;
    }

    public function process(\DateTimeInterface $lastOrderDate, ?\DateTimeInterface $result)
    {
        $dateTime = new \DateTime($lastOrderDate->format('Y-m-d'));

        // Calculate your next delivery/order date
        // Example: $dateTime->add(new \DateInterval('P5M'));

        return $dateTime;
    }
}
<!-- <plugin root>/src/Resources/config/services.xml -->

<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
           <service id="B2bSellersExample\ProductSubscription\DeliveryInterval\Processor\CustomIntervalProcessor">
            <tag name="b2b_product_subscription.interval.processor"/>
        </service>
    </services>
</container>

Last updated