[SOLVED] Sort products by price in Sylius REST API

Hello,
please how do I enable sorting by price in Sylius REST API?

I tried to override Product grid in AdminApiBundle by adding new field:

fields:
    price:
        type: int
        sortable: channelPricing.price

So I basically just copied this field from Product grid definition in ShopBundle.

But now I’m getting this error:

"[Semantical Error] line 0, col 146 near 'channelPricing.price': Error: 'channelPricing' is not defined."

Where & how should I define channelPricing?

Besides adding the field to the grid, you need to have it in the query builder.
See how it’s done for the shop bundle’s product grid

It uses Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository::createShopListQueryBuilder where the following lines allow filtering by price:

// Grid hack, we do not need to join these if we don't sort by price
    if (isset($sorting['price'])) {
        $queryBuilder
            ->innerJoin('o.variants', 'variant')
            ->innerJoin('variant.channelPricings', 'channelPricing')
            ->andWhere('channelPricing.channelCode = :channelCode')
            ->setParameter('channelCode', $channel->getCode())
        ;
    }
2 Likes

got it! Thank you :slight_smile: