# Customizing Subscription Intervals

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.

## Adding or Removing Intervals

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`.

### Example

{% code expandable="true" %}

```php
// <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
        ));
    }
}
```

{% endcode %}

## Implementing the Interval Processor

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.

### Example

{% code expandable="true" %}

```php
// <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;
    }
}
```

{% endcode %}

```xml
<service id="B2bSellersExample\ProductSubscription\DeliveryInterval\Processor\CustomIntervalProcessor">
    <tag name="b2b_product_subscription.interval.processor"/>
</service>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.b2b-sellers.com/b2b-platform/developer-guide/how-to/customizing-subscription-intervals.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
