Logo de Béjean Développement

Créer des jeux d'attributs à l'installation d'un module

Objectif

Développer un module permettant de créer des jeux d'attributs à l'installation d'un module.

Tutoriel

Création du module

Au sein du dossier app/code/, les dossiers suivants :

  • NicolasBejean\CreateAttributeSet\etc
  • NicolasBejean\CreateAttributeSet\Setup

Insérer le contenu suivant dans le fichier composer.json :

{
  "name": "nicolasbejean/createattributesset",
  "description": "Module de création des jeux d'attributs",
  "require": {
    "php": "~5.5.0|~5.6.0|~7.0.0"
  },
  "type": "magento2-module",
  "version": "1.0.0",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "autoload": {
    "files": [
      "registration.php"
    ]
  }
}

Insérer le contenu suivant dans le fichier registration.php :

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_CreateAttributeSet',
    __DIR__
);

Créer le fichier etc/module.xml et insérer le contenu suivant :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="NicolasBejean_CreateAttributeSet" setup_version="1.0.0" />
</config>

Terminer en insérant le contenu suivant dans le fichier Setup/InstallData.php :

<?php
namespace NicolasBejean\CreateAttributeSet\Setup;

use Magento\Catalog\Model\Product;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Model\AttributeSetManagement;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Exception\LocalizedException;

/**
 * Class InstallData
 * @package NicolasBejean\CreateAttributeSet\Setup
 */
class InstallData implements InstallDataInterface
{
    /**
     * @var EavSetup
     */
    private $_eavSetup;

    /**
     * @var AttributeSetManagement
     */
    private $_attributeSetManagement;

    /**
     * @var AttributeSetFactory
     */
    private $_attributeSetFactory;

    /**
     * @var ModuleDataSetupInterface
     */
    private $_setup;

    public function __construct(
        EavSetup $eavSetup,
        AttributeSetManagement $attributeSetManagement,
        AttributeSetFactory $attributeSetFactory
    )
    {
        $this->_eavSetup               = $eavSetup;
        $this->_attributeSetManagement = $attributeSetManagement;
        $this->_attributeSetFactory    = $attributeSetFactory;
    }

    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface   $context
     * @throws LocalizedException
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    )
    {
        $this->_setup = $setup;
        $this->_setup->startSetup();

        $this->createAttributeSet('Mon jeu d\'attributs');

        $this->_setup->endSetup();
    }

    /**
     * Créé un jeu d'attributs avec le squelette du jeu d'attribut par défaut
     *
     * @param $attributeSetName
     * @throws LocalizedException
     */
    private function createAttributeSet($attributeSetName)
    {
        if (!$this->isAttributeSetExists($attributeSetName)) {
            $defaultAttributeSetId = $this->_eavSetup->getDefaultAttributeSetId(Product::ENTITY);

            $attributeSet = $this->_attributeSetFactory->create();
            $data = [
                'attribute_set_name' => $attributeSetName,
                'entity_type_id'     => Product::ENTITY
            ];
            $attributeSet->setData($data);
            $attributeSet->validate();
            $this->_attributeSetManagement->create(Product::ENTITY, $attributeSet, $defaultAttributeSetId);
        }
    }

    /**
     * Vérifie si le jeu d'attributs existe
     *
     * @param $attributeSetName
     * @return bool
     */
    private function isAttributeSetExists($attributeSetName)
    {
        if (is_array($this->_eavSetup->getAttributeSet(Product::ENTITY, $attributeSetName))) {
            return true;
        } else {
            return false;
        }
    }
}

Lancer la commande d'installation pour créer le jeu d'attributs "Mon jeu d'attributs" : php bin/magento setup:upgrade