Скрипт для импорта наборов атрибутов и связанных атрибутов из CSV-файла
Создаем файл import_attribute_sets.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
$eavSetupFactory = $obj->get('Magento\Eav\Setup\EavSetupFactory');
$eavSetup = $eavSetupFactory->create(['setup' => $obj->get('Magento\Framework\Setup\ModuleDataSetupInterface')]);
$attributeSetRepository = $obj->get('Magento\Eav\Api\AttributeSetRepositoryInterface');
$attributeSetFactory = $obj->get('Magento\Eav\Api\Data\AttributeSetInterfaceFactory');
$attributeGroupFactory = $obj->get('Magento\Eav\Api\Data\AttributeGroupInterfaceFactory');
$attributeRepository = $obj->get('Magento\Eav\Api\AttributeRepositoryInterface');
$entityTypeId = $obj->get('Magento\Eav\Model\Entity\Type')->loadByCode(Product::ENTITY)->getId();
$csvFile = 'attribute_sets_export.csv';
if (!file_exists($csvFile)) {
echo "CSV file not found: $csvFile\n";
exit(1);
}
// Load CSV file
if (($handle = fopen($csvFile, 'r')) !== FALSE) {
$header = fgetcsv($handle, 1000, ',');
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
$row = array_combine($header, $data);
$attributeSetName = $row['attribute_set_name'];
$attributeGroupName = $row['attribute_group_name'];
$attributeCode = $row['attribute_code'];
// Check if attribute set exists
$attributeSetCollection = $obj->create('Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection')
->setEntityTypeFilter($entityTypeId)
->addFieldToFilter('attribute_set_name', $attributeSetName);
if ($attributeSetCollection->count() == 0) {
// Create new attribute set
$attributeSet = $attributeSetFactory->create();
$attributeSet->setEntityTypeId($entityTypeId)
->setAttributeSetName($attributeSetName);
$eavSetup->addAttributeSet($entityTypeId, $attributeSetName);
$attributeSetCollection = $obj->create('Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection')
->setEntityTypeFilter($entityTypeId)
->addFieldToFilter('attribute_set_name', $attributeSetName);
}
$attributeSet = $attributeSetCollection->getFirstItem();
// Check if attribute group exists
$attributeGroupCollection = $obj->create('Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection')
->setAttributeSetFilter($attributeSet->getId())
->addFieldToFilter('attribute_group_name', $attributeGroupName);
if ($attributeGroupCollection->count() == 0) {
// Create new attribute group
$eavSetup->addAttributeGroup($entityTypeId, $attributeSet->getId(), $attributeGroupName, 100);
$attributeGroupCollection = $obj->create('Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection')
->setAttributeSetFilter($attributeSet->getId())
->addFieldToFilter('attribute_group_name', $attributeGroupName);
}
$attributeGroup = $attributeGroupCollection->getFirstItem();
// Assign attribute to attribute group
try {
$attribute = $attributeRepository->get(Product::ENTITY, $attributeCode);
$eavSetup->addAttributeToGroup(
$entityTypeId,
$attributeSet->getId(),
$attributeGroup->getId(),
$attribute->getId(),
100
);
echo "Attribute $attributeCode assigned to group $attributeGroupName in set $attributeSetName.\n";
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
echo "Attribute $attributeCode does not exist.\n";
}
}
fclose($handle);
} else {
echo "Unable to open CSV file.\n";
}
Копируем в корень М2 вместе с CSV файлом из https://forum.hobby.vg/d/440-export-attribute-sets-magento-2-csv
В терминале (копировал и запускал под root)
php import_attribute_sets.php
Далее php bin/magento setup:upgrade
Далее php bin/magento setup:di:compile
Далее php bin/magento setup:static-content:deploy