integration_documentation:plugin:en:integration:shopware_5:extension

integration_documentation:plugin:en:integration:shopware_5:extension

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
integration_documentation:plugin:en:integration:shopware_5:extension [2021/12/02 18:05]
daniel
integration_documentation:plugin:en:integration:shopware_5:extension [2022/04/21 12:17]
tobi Add documentation for custom sorting
Line 236: Line 236:
     ```     ```
  
-The JSON could look like:+The exported variant ​JSON could look like:
  
 ```json ```json
Line 264: Line 264:
 } }
 ``` ```
 +
 +## (Requires 11.5.2) Add custom sorting options for API Integration (WIP)
 +
 +The Findologic base plugin already provides sorting options for the most common use-cases. The plugin uses `SortingHandler` to send the currently selected sorting option via API parameters to the Findologic Search-API (see [all available SortingHandlers](https://​github.com/​findologic/​plugin-shopware-5/​tree/​main/​FinSearchUnified/​Bundle/​SearchBundleFindologic/​SortingHandler)).
 +
 +Therefore to handle custom sorting options, create a custom `SortingHandler` in the extension plugin, and override the responsible `QueryBuilderFactory` to include the created `SortingHandler`.
 +
 +Supported Shopware 5 sortings by default:
 +- Release date
 +- Popularity
 +- Product name
 +- Price
 +- Relevance
 +
 +### Prerequisites
 +
 +Before a custom sorting can be used, make sure to export the value for the custom sort in the `<​sort>​` field in the export. See the [XML Format documentation](:​xml_export_documentation:​xml_format#​sorts) for further details.
 +
 +### Implementation
 +
 +Example implementation for sorting according to the stock value. This can be done for any non-supported sorting option, or with some sorting from a third-party plugin.
 +
 +**Step 1: Export the sort value**
 +
 +The export of the stock value needs to be added to `ExtendFinSearchUnified/​BusinessLogic/​Models/​FindologicArticleModel.php`
 +
 +```php
 +<?php
 +
 +namespace ExtendFinSearchUnified\BusinessLogic\Models;​
 +
 +use FINDOLOGIC\Export\Data\Sort;​
 +use FinSearchUnified\BusinessLogic\Models\FindologicArticleModel as OriginalFindologicArticleModel;​
 +use Shopware\Models\Article\Article;​
 +use Shopware\Models\Category\Category;​
 +
 +class FindologicArticleModel extends OriginalFindologicArticleModel
 +{
 +    public function __construct(
 +        Article $shopwareArticle,​
 +        $shopKey,
 +        array $allUserGroups,​
 +        array $salesFrequency,​
 +        Category $baseCategory
 +    ) {
 +        parent::​__construct($shopwareArticle,​ $shopKey, $allUserGroups,​ $salesFrequency,​ $baseCategory);​
 +
 +        $this->​setSort();​
 +    }
 +
 +    public function setSort()
 +    {
 +        $sort = new Sort();
 +        $sort->​setValue($this->​baseVariant->​getInStock());​
 +
 +        $this->​xmlArticle->​setSort($sort);​
 +    }
 +}
 +```
 +
 +**Step 2: Find the correct Shopware/​Third-party sorting class**
 +
 +The available Shopware sorting classes can be found [here](https://​github.com/​shopware/​shopware/​tree/​5.7/​engine/​Shopware/​Bundle/​SearchBundle/​Sorting). This is needed for the compatibility check inside the `SortingHandler`.
 +
 +Using `ProductStockSorting` for this example.
 +
 +**Step 2: Create a `SortingHandler`**
 +
 +Create the folder structure `Bundle/​SearchBundleFindologic/​SortingHandler` in the extension plugin, and add a custom sorting handler class. In this example it will be `ProductStockSortingHandler`:​
 +
 +```php
 +<?php
 +
 +namespace ExtendFinSearchUnified\Bundle\SearchBundleFindologic\SortingHandler;​
 +
 +use FinSearchUnified\Bundle\SearchBundleFindologic\QueryBuilder\QueryBuilder;​
 +use FinSearchUnified\Bundle\SearchBundleFindologic\SortingHandlerInterface;​
 +use Shopware\Bundle\SearchBundle\Sorting\ProductStockSorting;​
 +use Shopware\Bundle\SearchBundle\SortingInterface;​
 +use Shopware\Bundle\StoreFrontBundle\Struct\ShopContextInterface;​
 +
 +class ProductStockSortingHandler implements SortingHandlerInterface
 +{
 +    /**
 +     * Checks if the passed sorting can be handled by this class
 +     *
 +     * @param SortingInterface $sorting
 +     *
 +     * @return bool
 +     */
 +    public function supportsSorting(SortingInterface $sorting)
 +    {
 +        return $sorting instanceof ProductStockSorting;​
 +    }
 +
 +    /**
 +     * Handles the passed sorting object.
 +     *
 +     * @param SortingInterface $sorting
 +     * @param QueryBuilder $query
 +     * @param ShopContextInterface $context
 +     */
 +    public function generateSorting(SortingInterface $sorting, QueryBuilder $query, ShopContextInterface $context)
 +    {
 +        /** @var ProductStockSorting $sorting */
 +        $query->​addOrder('​shopsort ' . $sorting->​getDirection());​
 +    }
 +}
 +```
 +
 +**Step 3: Decorate the `QueryBuilderFactory`**
 +
 +The `QueryBuilderFactory` registers all the available sorting options. Therefore, you want to add your custom sort to this factory.
 +Create the folder structure `Bundle/​SearchBundleFindologic/​QueryBuilder` in the extension plugin, and add the file `QueryBuilderFactory`. Override the `registerSortingHandlers` method and add your handler to the array.
 +
 +```php
 +<?php
 +
 +namespace ExtendFinSearchUnified\Bundle\SearchBundleFindologic\QueryBuilder;​
 +
 +use ExtendFinSearchUnified\Bundle\SearchBundleFindologic\SortingHandler\ProductStockSortingHandler;​
 +use FinSearchUnified\Bundle\SearchBundleFindologic\QueryBuilder\QueryBuilderFactory as OriginalQueryBuilderFactory;​
 +use FinSearchUnified\Bundle\SearchBundleFindologic\SortingHandlerInterface;​
 +
 +class QueryBuilderFactory extends OriginalQueryBuilderFactory
 +{
 +    /**
 +     * @return SortingHandlerInterface[]
 +     */
 +    protected function registerSortingHandlers()
 +    {
 +        $sortingHandlers = parent::​registerSortingHandlers();​
 +
 +        $sortingHandlers[] = new ProductStockSortingHandler();​
 +
 +        return $sortingHandlers;​
 +    }
 +}
 +```
 +
 +
 +**Step 4: Add the decorated factory to the services.xml**
 +
 +As a last step, simply decorate it in your `Resources/​services.xml`:​
 +
 +```xml
 +<?xml version="​1.0"​ encoding="​utf-8"?>​
 +<​container xmlns="​http://​symfony.com/​schema/​dic/​services"​
 +           ​xmlns:​xsi="​http://​www.w3.org/​2001/​XMLSchema-instance"​
 +           ​xsi:​schemaLocation="​http://​symfony.com/​schema/​dic/​services http://​symfony.com/​schema/​dic/​services/​services-1.0.xsd">​
 +    <​services>​
 +        <service
 +            id="​extend_fin_search_unified.article_model_factory"​
 +            class="​ExtendFinSearchUnified\BusinessLogic\FindologicArticleFactory"​
 +            decorates="​fin_search_unified.article_model_factory"​
 +        >
 +        </​service>​
 +
 +        <service
 +            id="​extend_fin_search_unified.query_builder_factory"​
 +            class="​ExtendFinSearchUnified\Bundle\SearchBundleFindologic\QueryBuilder\QueryBuilderFactory"​
 +            decorates="​fin_search_unified.query_builder_factory"​
 +        >
 +            <​argument type="​service"​ id="​shopware_plugininstaller.plugin_manager"​ />
 +            <​argument type="​service"​ id="​config"​ />
 +        </​service>​
 +    </​services>​
 +</​container>​
 +```
 +
 +Once this step is done, selecting your relevant sorting option will send the order parameter to the Findologic API.
  
 </​markdown>​ </​markdown>​