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
integration_documentation:plugin:en:integration:shopware_5:extension [2021/12/02 17:42]
daniel
integration_documentation:plugin:en:integration:shopware_5:extension [2022/04/21 18:17] (current)
daniel
Line 12: Line 12:
 1. Download the latest zip file from our [GitHub release](https://​github.com/​findologic/​plugin-shopware-5-extension/​releases) page.  1. Download the latest zip file from our [GitHub release](https://​github.com/​findologic/​plugin-shopware-5-extension/​releases) page. 
  
-2. Open the Plugin Manager.+2. Open the Plugin Manager ​in the Shopware backend.
  
 3. Click **Upload plugin** and select downloaded zip file. 3. Click **Upload plugin** and select downloaded zip file.
Line 168: Line 168:
 ## Add a custom free text field ## Add a custom free text field
  
-As stated ​[here](https://​docs.findologic.com/​doku.php?​id=integration_documentation:​plugin:​en:​integration:​shopware_5:​export_information#​attributes), due to a limitation of Shopware, only the free text field from `attr1` to `attr20` ​are exported by defaultAdditionally added free text fields ​need to be exported ​manually.+The default ​[attributes export](https://​docs.findologic.com/​doku.php?​id=integration_documentation:​plugin:​en:​integration:​shopware_5:​export_information#​attributes) ​contains ​the free text field from `attr1` to `attr20`. ​Additional and custom ​free text fields ​can be exported ​using the example below.
  
 ```php ```php
Line 220: Line 220:
 ## Add variant data to the export ## Add variant data to the export
  
-The default extension plugin already includes the code to export variants as a JSON string, ​this code just needs a small adaption:+The default extension plugin already includes the code to export variants as a JSON string, ​the provided ​code just needs a small adaption:
  
 1. Check out the variant configuration and get the property name, which defines your variants. 1. Check out the variant configuration and get the property name, which defines your variants.
Line 236: Line 236:
     ```     ```
  
-### Example ​JSON+The exported variant ​JSON could look like:
  
 ```json ```json
Line 264: Line 264:
 } }
 ``` ```
 +
 +## Add custom sorting options for API Integration
 +
 +</​markdown>​
 +<note important>​Requires Findologic Plugin v11.5.2 or above</​note>​
 +<​markdown>​
 +
 +The Findologic 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)).
 +
 +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. 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**
 +
 +Finally 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>​