Back to Top

How to extend the entity at the Shopware

Updated 16 August 2020

In this blog, you are going to learn “ how to extend the entity at the Shopware ”.
I hope you know the directory structure of Shopware 6 plugin, if you don’t know, see here- https://docs.shopware.com/en/shopware-platform-dev-en/internals/directory-structure.

Own entities can be integrated into the core via the corresponding entry in the services.xml.To extend existing entities, the abstract class \Shopware\Core\Framework\DataAbstractionLayer\EntityExtension is used. The EntityExtension must define which entity should be extended in the getDefinitionClass method. Once this extension access in the system, the extension can add more fields to it:

<?php declare(strict_types=1);

namespace Webkul\test\Core\Content\Extension;

use Shopware\Core\Framework\DataAbstractionLayer\EntityExtension;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToManyAssociationField;
use Webkul\test\Core\Content\test\testDefinition;
use Shopware\Core\Content\Product\Aggregate\ProductReview\ProductReviewDefinition;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;

class ProductExtension extends EntityExtension
{
    public function extendFields(FieldCollection $collection): void
    {
        $collection->add(
            (new OneToManyAssociationField('test', testDefinition::class, 'product_id'))
        );
    }

    public function getDefinitionClass(): string
    {
        return ProductDefinition::class;
    }
}

This example adds another association named `OneToManyAssociationField` to the ProductDefinition.  You can add any association with them or object field or whatever you want to add in the definition. You can learn about the flags or find out which field types are available in Shopware.

So, time to take care of the product entities’ new field yourself. You’re going to need a new subscriber for this. Have a look here to find out how to properly add your own subscriber class.

<?php declare(strict_types=1);

namespace Webkul\test\Subscriber;

use Shopware\Storefront\Page\Product\ProductLoaderCriteriaEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrderedProductReviewSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            ProductLoaderCriteriaEvent::class => 'onProductCriteriaLoaded'
        ];
    }

    public function onProductCriteriaLoaded(ProductLoaderCriteriaEvent $event): void
    {
        $event->getCriteria()->addAssociation('test');
    }
}

As you can see, the subscriber listens to the ProductLoaderCriteriaEvent event, which is triggered every time a set of products is requested. The listener onProductCriteriaLoaded then add an association with them. This is a good way to add your own entities with core entities and wherever you can add your data and display them. There are so many events of a product that helps us to modify the criteria according to them.
So whenever you creating your own page add generic page loader and create an event to them so that you can simply add your data.

Start your headless eCommerce
now.
Find out More

services.xml:

<?xml version="1.0" ?>

<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="Webkul\test\Subscriber\testSubscriber">
            <tag name="kernel.event_subscriber"/>
        </service>
         <service id="Webkul\test\Core\Content\Extension\ProductExtension">
            <tag name="shopware.entity.extension"/>
        </service>
    </services> 
</container>

If you are adding the entity extension file in the service file so make sure tag name `shopware.entity.extension`.

I hope it will help you. Thanks for reading. Happy Coding 🙂
Thank You.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home