Skip to main content

Shopware6 - Storefront - add additional product information from associations

Category: Shopware
shopware-logo

A short example how to access additional product information in Shopware6 Storefront from associations.

Subscriber

<?php
declare(strict_types=1);

namespace contribName\PluginName\Subscriber;

use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;


class ProductListingCriteria implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            ProductListingCriteriaEvent::class => 'extendProductListingCriteria'
        ];
    }

    public function extendProductListingCriteria(ProductListingCriteriaEvent $event) :void
    {
        $criteria = $event->getCriteria();

        $criteria->addAssociation('configuratorSettings')
            ->addAssociation('configuratorSettings.option')
            ->addAssociation('configuratorSettings.option.media')
            ->addAssociation('configuratorSettings.option.productOptions')
            ;
    }
}

Twig

        {% for variant in product.configuratorSettings.elements %}
            {%  if variant.option.colorHexCode %}
                <div class="color-variants--option" style="background-color: {{  variant.option.colorHexCode }}" title="{{ variant.option.name }}">

                </div>
            {%  endif %}
        {% endfor %}