Skip to content

List products in catalog

To list products from a specific catalog, you can either use the Content query Field, or a custom controller.

List products with Content query Field

To list products from a catalog in a Content query Field, add this Field to a Content Type definition.

Select the "Catalog" Query type. In parameters, provide the identifier of the catalog.

Configuration of Content query Type for catalog

Save the Content Type definition and create a Content item based on it. The contents of the catalog are rendered automatically in the Field.

See Content query Field for more information.

List products with custom controller

You can also customize the rendering of products from a selected catalog with a custom controller and render it in a custom route.

In this example, create a custom controller for a promo catalog in src/Controllers/PromoController.php:

 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
30
31
32
33
34
35
<?php

declare(strict_types=1);

namespace App\Controller;

use Ibexa\Bundle\Core\Controller;
use Ibexa\Contracts\ProductCatalog\CatalogServiceInterface;
use Ibexa\Contracts\ProductCatalog\Values\Product\ProductQuery;
use Ibexa\Contracts\ProductCatalog\ProductServiceInterface;

final class PromoController extends Controller
{
    private ProductServiceInterface $productService;

    private CatalogServiceInterface $catalogService;

    public function __construct(ProductServiceInterface $productService, CatalogServiceInterface $catalogService)
    {
        $this->productService = $productService;
        $this->catalogService = $catalogService;
    }

    public function listPromoProductsAction()
    {
        $catalog = $this->catalogService->getCatalogByIdentifier('desk_promo');
        $query = new ProductQuery($catalog->getQuery());

        $result = $this->productService->findProducts($query);

        return $this->render('@ibexadesign/full/promo.html.twig', [
            'products' => $result,
        ]);
    }
}

The controller uses CatalogService to get the selected catalog by its identifier, desk_promo (line 26).

The catalog provides a query, based on the filters you configured. You can get the query with $catalog->getQuery() and use it as criteria in product query (lines 27-29).

Next, add a route for rendering the promo content which points to the controller:

1
2
3
4
promo:
    path: /promo
    defaults:
        _controller: App\Controller\PromoController::listPromoProductsAction

Finally, add a template to render a table with the products covered by the promo:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<h1>Special offer</h1>

<table>
    {% for product in products %}
        <tr>
            <td><a href="{{  path('ibexa.url.alias', {'contentId': product.content.contentInfo.id}) }}">{{ (product.name) }}</a></td>
            <td>{{ product.price }}</td>
            <td>
                <form method="POST" action="{{ path('ibexa.commerce.basket.add') }}">
                <input type="hidden" name="ses_basket[0][sku]" value="{{ product.code }}"/>
                <input type="hidden" name="ses_basket[0][quantity]" value="1"/>
                <button>Add to basket</button>
                </form>
            </td>
        </tr>
    {% endfor %}
</table>

You can now preview the <youdomain>/promo page and view the products in the catalog.

Note

The anonymous user does not have permission to view products and catalogs. To view the catalog and its products, you need to either log in, or give the anonymous user the product/view and catalog/view Policies.