Customizing Subscription Intervals
Last updated
Was this helpful?
The B2Bsellers Suite allows you to extend or modify the available subscription intervals to meet specific business requirements. This is a two-step process: adding the interval to the collection and providing a processor to calculate future delivery dates.
To modify the list of available intervals, create an event subscriber that listens to the DeliveryIntervalCollectorEvent.
Inside the subscriber, you can use the getCollection() method to remove existing intervals (like weekly) or add new ones using the DeliveryIntervalStruct.
For every custom interval added, you must provide a corresponding processor to calculate the next delivery or order date.
The processor must implement the IntervalProcessorInterface and be registered in the service container with a specific the b2b_product_subscription.interval.processor tag.
Last updated
Was this helpful?
Was this helpful?
// <plugin root>/src/Subscriber/DeliveryIntervalSubscriber.php
namespace B2bSellersExample\Subscriber;
use B2bProductSubscription\Components\ProductSubscription\Events\DeliveryIntervalCollectorEvent;
use B2bProductSubscription\Components\ProductSubscription\Struct\DeliveryIntervalStruct;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
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): void
{
$collection = $event->getCollection();
// Remove an existing interval
$collection->remove('weekly');
// Add a custom interval
$key = 'custom';
$collection->set($key, new DeliveryIntervalStruct(
$key,
$this->translator->trans('b2b-subscription.interval.' . $key),
'b2b-subscription.interval.' . $key
));
}
}// <plugin root>/src/ProductSubscription/DeliveryInterval/Processor/CustomIntervalProcessor.php
namespace B2bSellersExample\ProductSubscription\DeliveryInterval\Processor;
use B2bProductSubscription\Components\ProductSubscription\DeliveryInterval\Processor\IntervalProcessorInterface;
class CustomIntervalProcessor implements IntervalProcessorInterface
{
public const DELIVERY_KEY = 'custom';
public function isSupported(string $key): bool
{
return self::DELIVERY_KEY === $key;
}
public function process(\DateTimeInterface $lastOrderDate, ?\DateTimeInterface $result): \DateTimeInterface
{
$dateTime = new \DateTime($lastOrderDate->format('Y-m-d'));
// Logic: Add 5 months for the next delivery
$dateTime->add(new \DateInterval('P5M'));
return $dateTime;
}
}<service id="B2bSellersExample\ProductSubscription\DeliveryInterval\Processor\CustomIntervalProcessor">
<tag name="b2b_product_subscription.interval.processor"/>
</service>