ActiveDateRange example

/** * Gets a criteria for all permitted promotions of the provided * sales channel context, that do require an individual code * and have not yet been used in an order. * * @param list<string> $codes */
    public function __construct(
        array $codes,
        string $salesChannelId
    ) {
        $activeDateRange = new ActiveDateRange();

        parent::__construct(
            MultiFilter::CONNECTION_AND,
            [
                new EqualsFilter('active', true),
                new EqualsFilter('promotion.salesChannels.salesChannelId', $salesChannelId),
                $activeDateRange,
                new EqualsFilter('useCodes', true),
                new EqualsFilter('useIndividualCodes', true),
                new EqualsAnyFilter('promotion.individualCodes.code', $codes),
                // a payload of null means, they have not yet been redeemed
/** * @param list<string> $codes */
    private function getExpectedFilter(array $codes): MultiFilter
    {
        return new MultiFilter(
            MultiFilter::CONNECTION_AND,
            [new EqualsFilter('active', true),
                new EqualsFilter('promotion.salesChannels.salesChannelId', $this->salesChannel->getId()),
                // yes, i know, this is not the best isolation, but its actually what we want                 new ActiveDateRange(),
                new EqualsFilter('useCodes', true),
                new EqualsFilter('useIndividualCodes', false),
                new EqualsAnyFilter('code', $codes),
            ]
        );
    }
}

#[Package('buyers-experience')] class PermittedAutomaticPromotions extends MultiFilter
{
    /** * Gets a criteria for all permitted promotions of the provided * sales channel context, that do get applied automatically without a code * if the preconditions and rules are valid. */
    public function __construct(string $salesChannelId)
    {
        $activeDateRange = new ActiveDateRange();

        // add conditional OR filter to either get an entry that matches any existing rule,         // or promotions that don't have ANY rules and thus are used globally         parent::__construct(
            MultiFilter::CONNECTION_AND,
            [
                new EqualsFilter('active', true),
                new EqualsFilter('promotion.salesChannels.salesChannelId', $salesChannelId),
                $activeDateRange,
                new EqualsFilter('useCodes', false),
            ]
        );

    /** * Gets a criteria for all permitted promotions of the provided * sales channel context, that do require a global code. * * @param list<string> $codes */
    public function __construct(
        array $codes,
        string $salesChannelId
    ) {
        $activeDateRange = new ActiveDateRange();

        parent::__construct(
            MultiFilter::CONNECTION_AND,
            [new EqualsFilter('active', true),
                new EqualsFilter('promotion.salesChannels.salesChannelId', $salesChannelId),
                $activeDateRange,
                new EqualsFilter('useCodes', true),
                new EqualsFilter('useIndividualCodes', false),
                new EqualsAnyFilter('code', $codes),
            ]
        );
    }
/** * @param list<string> $codes */
    private function getExpectedFilter(array $codes): MultiFilter
    {
        return new MultiFilter(
            MultiFilter::CONNECTION_AND,
            [
                new EqualsFilter('active', true),
                new EqualsFilter('promotion.salesChannels.salesChannelId', $this->salesChannel->getId()),
                new ActiveDateRange(),
                new EqualsFilter('useCodes', true),
                new EqualsFilter('useIndividualCodes', true),
                new EqualsAnyFilter('promotion.individualCodes.code', $codes),
                // a payload of null means, they have not yet been redeemed                 new EqualsFilter('promotion.individualCodes.payload', null),
            ]
        );
    }
}
static::assertEquals($this->getExpectedFilter()->getQueries()$template->getQueries());
    }

    private function getExpectedFilter(): MultiFilter
    {
        return new MultiFilter(
            MultiFilter::CONNECTION_AND,
            [
                new EqualsFilter('active', true),
                new EqualsFilter('promotion.salesChannels.salesChannelId', $this->salesChannel->getId()),
                // yes, i know, this is not the best isolation, but its actually what we want                 new ActiveDateRange(),
                new EqualsFilter('useCodes', false),
            ]
        );
    }
}

class ActiveDateRangeTest extends TestCase
{
    /** * This test verifies, that we get the * expected and defined criteria from the template. * * @group promotions */
    public function testCriteria(): void
    {
        $template = new ActiveDateRange();

        static::assertEquals($this->getExpectedDateRangeFilter()->getQueries()$template->getQueries());
    }

    /** * @throws \Exception */
    private function getExpectedDateRangeFilter(): MultiFilter
    {
        $today = new \DateTime();
        $today = $today->setTimezone(new \DateTimeZone('UTC'));

        
Home | Imprint | This part of the site doesn't use cookies.