PHP code example of bestit / commercetools-filter-bundle

1. Go to this page and download the library: Download bestit/commercetools-filter-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bestit / commercetools-filter-bundle example snippets



// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new BestIt\Commercetools\FilterBundle\BestItCommercetoolsFilterBundle(),
        );

        // ...
    }

    // ...
}

// ListingController.php

    public function indexAction(Request $request, Category $category): array
    {
        $result = $this->get('best_it_commercetools_filter.manager.filter_manager')->listing($request, $category);
        
        return [
            'products' => $result->getProducts(),
            'totalProducts' => $result->getTotalProducts(),
            'context' => $result->getContext(),
            'pagination' => $result->getPagination(),
            'sorting' => $result->getSorting(),
            'facetForm' => $result->getForm()
        ];
    }

// SearchController.php

    public function indexAction(Request $request, Category $category): array
    {
        $result = $this->get('best_it_commercetools_filter.manager.filter_manager')->search($request, $request->query->get('search'));
        
        return [
            'products' => $result->getProducts(),
            'totalProducts' => $result->getTotalProducts(),
            'context' => $result->getContext(),
            'pagination' => $result->getPagination(),
            'sorting' => $result->getSorting(),
            'facetForm' => $result->getForm()
        ];
    }

// FilterRequestSubscriber.php

class FilterRequestSubscriber implements EventSubscriberInterface
{
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents()
    {
        return [
            SuggestEvent::PRODUCTS_REQUEST_POST => 'onProductsFilterRequest',
            FilterEvent::PRODUCTS_REQUEST_POST => 'onProductsSuggestRequest'
        ];
    }

    /**
     * On products request for filter
     * @param ProductProjectionSearchRequestEvent $event
     */
    public function onProductsFilterRequest(ProductProjectionSearchRequestEvent $event)
    {
        $request = $event->getRequest();

        $request
            ->expand('masterVariant.attributes[*].value')
            ->expand('productType');

        $event->setRequest($request);
    }

    /**
     * On products request for suggest
     * @param ProductProjectionSearchRequestEvent $event
     */
    public function onProductsSuggestRequest(ProductProjectionSearchRequestEvent $event)
    {
        $request = $event->getRequest();

        $request
            ->expand('categories[*]')
            ->expand('masterVariant.attributes[*].value[*].value')
            ->expand('productType');

        $event->setRequest($request);
    }
}