• Изменено

Экспортировать наборы атрибутов (attribute sets) вместе с их связанными атрибутами в Magento 2 можно несколькими способами. Один из них – использование скрипта PHP. Такой скрипт поможет извлечь все необходимые данные и сохранить их в формате CSV или JSON, чтобы потом можно было импортировать эти данные в другой инстанс Magento.

Создаем файл export_attribute_sets.php с содежимым:

<?php
use Magento\Framework\App\Bootstrap;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\Group as AttributeGroup;

require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

// Retrieve necessary models
$attributeSetCollection = $obj->create('Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection')
    ->setEntityTypeFilter($obj->get('Magento\Eav\Model\Entity\Type')->loadByCode(Product::ENTITY)->getId());
$attributeSetRepository = $obj->create('Magento\Eav\Api\AttributeSetRepositoryInterface');
$attributeGroupCollectionFactory = $obj->create('Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory');
$attributeCollectionFactory = $obj->create('Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory');

$output = [];
$output[] = ['attribute_set_name', 'attribute_group_name', 'attribute_code'];

foreach ($attributeSetCollection as $attributeSet) {
    $attributeSetName = $attributeSet->getAttributeSetName();
    $attributeSetId = $attributeSet->getId();
    $attributeSetData = $attributeSetRepository->get($attributeSetId);
    
    $attributeGroups = $attributeGroupCollectionFactory->create()
        ->setAttributeSetFilter($attributeSetId)
        ->load();

    foreach ($attributeGroups as $attributeGroup) {
        $attributeGroupName = $attributeGroup->getAttributeGroupName();
        
        $attributes = $attributeCollectionFactory->create()
            ->setAttributeGroupFilter($attributeGroup->getId())
            ->load();
        
        foreach ($attributes as $attribute) {
            $output[] = [
                $attributeSetName,
                $attributeGroupName,
                $attribute->getAttributeCode()
            ];
        }
    }
}

// Write CSV
$fp = fopen('attribute_sets_export.csv', 'w');
foreach ($output as $row) {
    fputcsv($fp, $row);
}
fclose($fp);

echo "Attribute sets exported successfully to attribute_sets_export.csv\n";

Перемещаем его в корень М2.
В терминале (копировал и запускал под root)
php export_attribute_sets.php
В корне создается файл CSV с наборами атрибутов.

Как импортировать? Смотреть здесь https://forum.hobby.vg/d/441-import-attribute-sets-magento-2-csv