Skip to content

Discounts Search Sort Clauses reference

Sort Clauses are found in the Ibexa\Contracts\Discounts\Value\Query\SortClause namespace, implementing the SortClauseInterface interface:

Name Description
CreatedAt Sort by discount's creation date
EndDate Sort by discount's end date
Id Sort by discount's database ID
Identifier Sort by discount identifier
Priority Sort by discount priority
StartDate Sort by discount start date
Type Sort by the place where the discount activates: catalog or cart. When sorting with ascending order, cart discounts are returned first.
UpdatedAt Sort by discount modification date

The following example shows how to use them to sort the searched discounts:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php

declare(strict_types=1);

use Ibexa\Contracts\CoreSearch\Values\Query\Criterion\FieldValueCriterion;
use Ibexa\Contracts\Discounts\Value\Query\Criterion;
use Ibexa\Contracts\Discounts\Value\Query\DiscountQuery;
use Ibexa\Contracts\Discounts\Value\Query\SortClause;

$now = new DateTimeImmutable();

$query = new DiscountQuery(
    new Criterion\LogicalAnd(
        new Criterion\IsEnabledCriterion(),
        new Criterion\StartDateCriterion($now, FieldValueCriterion::COMPARISON_LTE),
        new Criterion\LogicalOr(
            new Criterion\EndDateCriterion($now, FieldValueCriterion::COMPARISON_GTE),
            new Criterion\EndDateCriterion(null, FieldValueCriterion::COMPARISON_EQ)
        ),
    ),
    [
        new SortClause\Type(),
        new SortClause\Priority(),
        new SortClause\CreatedAt(),
    ]
);

/** @var \Ibexa\Contracts\Discounts\DiscountServiceInterface $discountService */
$results = $discountService->findDiscounts($query);

The returned active discounts are sorted by:

  • the place where they activate: catalog or cart, with cart discounts returned first
  • priority (descending)
  • creation date (descending)

You can change the default sorting order by using the SORT_ASC and SORT_DESC constants from AbstractSortClause.