How to prevent EntityWrittenEvent from triggering again and again?

If you want to subscribe an EntityWrittenEvent and update the same entity. the event sucriber will work as a loop.

In order to prevent this, you can use the Shopware\Core\Framework\DataAbstractionLayer\VersionManager

class

for eg:

public function update(array $data, Context $context)
    {
        $affected = $this->versionManager->update($this->productDefinition , $data, WriteContext::createFromContext($context));

    }

    public function upsert(array $data, Context $context)
    {
        $affected = $this->versionManager->upsert($this->productDefinition , $data, WriteContext::createFromContext($context));
    }


    public function create(array $data, Context $context)
    {
        $affected = $this->versionManager->insert($this->productDefinition, $data, WriteContext::createFromContext($context));
    }