Search code examples
phpshopwareshopware6

Is it possible to make a profile for a custom entity to import/export data with a CSV?


In shopware it is possible to import/export data with a profile. See the following link. https://docs.shopware.com/en/shopware-en/settings/importexport

enter image description here

The only problem now is that I want to upload data of a custom entity. I can't find the custom entity in the profiles. So my question is of it is possible to upload this data in bulk via an CSV?


Solution

  • You'll have to persist a import/export profile for your custom entity:

    $container->get('import_export_profile.repository')->create([
        [
            'name' => 'My Custom Entity',
            'label' => 'My Custom Entity',
            'sourceEntity' => 'my_custom_entity',
            'type' => ImportExportProfileEntity::TYPE_IMPORT_EXPORT,
            'fileType' => 'text/csv',
            'delimiter' => ';',
            'enclosure' => '"',
            'config' => [],
            'mapping' => [
                ['key' => 'id', 'mappedKey' => 'id', 'position' => 1],
                ['key' => 'active', 'mappedKey' => 'active', 'position' => 2],
                ['key' => 'translations.DEFAULT.name', 'mappedKey' => 'name', 'position' => 3],
                ['key' => 'type', 'mappedKey' => 'type', 'position' => 0],
            ],
        ]
    ], $context);
    

    There you have to map your entities fields to the columns of the CSV.

    If you want to influence how fields are written or read you might want to register a serializer for your entity. For example the product entity has a serializer:

    <service id="Shopware\Core\Content\ImportExport\DataAbstractionLayer\Serializer\Entity\ProductSerializer">
        <!-- ... -->
        <tag name="shopware.import_export.entity_serializer" priority="-400"/>
    </service>
    

    Maybe have a look at the serializer for reference if your entity has some fields that need special treatment.