integration_documentation:plugin:en:integration:shopware_5:extension

integration_documentation:plugin:en:integration:shopware_5:extension

Shopware 5 plugin extension

The Findologic extension plugin for Shopware 5 allows manual adaptions to the behavior of the main Findologic plugin.

It's possible override, extend and replace components, to fit the needs of your store. The most common use case is to add additional data to the Findologic product export.

  1. Download the latest zip file from our GitHub release page.

  2. Open the Plugin Manager in the Shopware backend.

  3. Click Upload plugin and select downloaded zip file.

Decorators

Adaptions need to be done using Symfony decorators.

By default the extension plugin decorates the FindologicArticleFactory which is responsible for creating new FindologicArticleModel instances. Those products are used by the export to build an XML.

Resources/services.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>
    </services>
</container>

FindologicArticleFactory

With the decoration in place, the plugin returns an extended FindologicArticleModel instance also defined in the extension plugin.

BusinessLogic/FindologicArticleFactory.php

<?php
 
namespace ExtendFinSearchUnified\BusinessLogic;
 
use ExtendFinSearchUnified\BusinessLogic\Models\FindologicArticleModel;
use Shopware\Models\Article\Article;
use Shopware\Models\Category\Category;
 
class FindologicArticleFactory
{
    public function create(Article $shopwareArticle, $shopKey, array $allUserGroups, array $salesFrequency, Category $baseCategory)
    {
        return new FindologicArticleModel($shopwareArticle, $shopKey, $allUserGroups, $salesFrequency, $baseCategory);
    }
}

FindologicArticleModel

This class can be used to customize functionality of the main plugin.

BusinessLogic\Models\FindologicArticleModel.php

<?php
 
namespace ExtendFinSearchUnified\BusinessLogic\Models;
 
use Exception;
use FINDOLOGIC\Export\Data\Property;
use FinSearchUnified\BusinessLogic\Models\FindologicArticleModel as OriginalFindologicArticleModel;
use FinSearchUnified\Helper\StaticHelper;
use Shopware\Bundle\MediaBundle\MediaService;
use Shopware\Models\Article\Article;
use Shopware\Models\Article\Detail;
use Shopware\Models\Article\Image;
use Shopware\Models\Category\Category;
use Shopware\Models\Media\Media;
 
class FindologicArticleModel extends OriginalFindologicArticleModel
{
    // The variant column that differentiates the different variants
    const VARIANT_TYPE = 'Farbe';
 
    const URL_REPLACEMENTS = [
        '[' => '%5B',
        ']' => '%5D'
    ];
 
    /**
     * @var MediaService|null
     */
    protected $mediaService;
 
    public function __construct(
        Article $shopwareArticle,
        $shopKey,
        array $allUserGroups,
        array $salesFrequency,
        Category $baseCategory
    ) {
        parent::__construct($shopwareArticle, $shopKey, $allUserGroups, $salesFrequency, $baseCategory);
 
//        $this->mediaService = Shopware()->Container()->get('shopware_media.media_service');
//
//        if ($this->legacyStruct) {
//            $this->addVariantsJson();
//        }
    }
 
    //...
}

Examples

You can manually add attributes and properties by adding them to $this->xmlArticle with the help of libflexport.

BusinessLogic\Models\FindologicArticleModel.php

use FINDOLOGIC\Export\Data\Attribute;
use FINDOLOGIC\Export\Data\Property;
 
// ...
public function setAttributes(): void
{
    parent::setAttributes();
 
    $this->xmlArticle->addAttribute(
        new Attribute(
            'filter_name',
            ['value1', 'value2']
        )
    );
}
 
public function setProperties(): void
{
    parent::setProperties();
 
    $this->xmlArticle->addProperty(
        new Property('new_property', ['' => 'I am a property value!'])
    );
}
// ...

The part ['' => 'I am a property value!'] has an empty string as array index. An empty string as an array key, simply means that there is no usergroup, as property data can be usergroup-specific.

You can read more about usergroups in the libflexport documentation, which is the library used to build the export xml.

In case you have usergroup-specific data, properties which should be available for all usergroups, must be added individually for each usergroup.

The default attributes export contains the free text field from attr1 to attr20. Additional and custom free text fields can be exported using the example below.

use FINDOLOGIC\Export\Data\Attribute;
use FINDOLOGIC\Export\Data\Property;
 
public function __construct(
    Article $shopwareArticle,
    $shopKey,
    array $allUserGroups,
    array $salesFrequency,
    Category $baseCategory
) {
    parent::__construct($shopwareArticle, $shopKey, $allUserGroups, $salesFrequency, $baseCategory);
 
    if ($this->legacyStruct) {
        $this->addAdditionalFreeTextFields();
    }
}
 
// ...
public function addAdditionalFreeTextFields(): void
{
    $freeTextFields = $this->baseVariant->getAttribute();
 
    // Add free text field as property (without usergroup)
    if(is_callable([$freeTextFields, 'getCustomRatingCount'])) {
        $customRatingCount = $freeTextFields->getCustomRatingCount();
        if (!StaticHelper::isEmpty($customRatingCount)) {
            $this->xmlArticle->addProperty(
                new Property('rating_count', ['' => StaticHelper::cleanString($customRatingCount)])
            );
        }
    }
 
    // Add free text field as attribute
    if(is_callable([$freeTextFields, 'getCustomRatingAvg'])) {
        $customRatingAvg = $freeTextFields->getCustomRatingAvg();
        if (!StaticHelper::isEmpty($customRatingAvg)) {
            $this->xmlArticle->addAttribute(
                new Attribute('rating_avg', [StaticHelper::cleanString($customRatingAvg)])
            );
        }
    }
}
// ...

The function names are dynamically generated by Shopware. They are accessible as PascalCase, prefixed with "get". So the getter for the free text field custom_rating_count, is generated as getCustomRatingCount.

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.

  2. Configure the variant name in the constant VARIANT_TYPE (line 19)

  3. Comment out the line 40-44

     //        $this->mediaService = Shopware()->Container()->get('shopware_media.media_service');
     //
     //        if ($this->legacyStruct) {
     //            $this->addVariantsJson();
     //        }

The exported variant JSON could look like:

{
    "red": {
        "productUrl": "https://www.example.com/de/jacket?number=SW10000-01",
        "images": [
            "https://www.example.com/media/image/73/bc/ef/jacket-red-01.jpg",
            "https://www.example.com/media/image/73/bc/ef/jacket-red-02.jpg",
        ],
        "thumbnails": [
            "https://www.example.com/media/image/73/bc/ef/jacket-red-01_200x200.jpg",
            "https://www.example.com/media/image/73/bc/ef/jacket-red-02_200x200.jpg",
        ],
    },
    "black": {
        "productUrl": "https://www.example.com/de/jacket?number=SW10000-02",
        "images": [
            "https://www.example.com/media/image/73/bc/ef/jacket-black-01.jpg",
            "https://www.example.com/media/image/73/bc/ef/jacket-black-02.jpg",
        ],
        "thumbnails": [
            "https://www.example.com/media/image/73/bc/ef/jacket-black-01_200x200.jpg",
            "https://www.example.com/media/image/73/bc/ef/jacket-black-02_200x200.jpg",
        ],
    }
}