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

Time:01-20

In shopware it is possible to import/export data with a profile. See the following link. 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?

CodePudding user response:

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.

CodePudding user response:

You have to add your entity to

vendor/shopware/administration/Resources/app/administration/src/module/sw-import-export/component/sw-import-export-edit-profile-general/index.js

to supportedEntieties() method

You can do it like this:

const { Component } = Shopware;

Component.override('sw-import-export-edit-profile-general', {
    methods: {
        supportedEntities() {
            const supportedEntities = this.$super('supportedEntities');
            supportedEntities.push({
                value: 'your_entity_name',
                label: "Entity Label",
                type: profileTypes.IMPORT_EXPORT,
            });
            
            return supportedEntities;
        }
    }
});
  • Related