So erstellen Sie ein Produktattribut und weisen ihm alle in Magento 2 festgelegten Attribute zu


7

Magento 2 - Wie erstelle ich ein Produktattribut und ordne ihm alle Attributgruppen mithilfe des Setup-Skripts zu?

Antworten:


10

Wenn Sie im Attributkonfigurationsarray einen Wert für 'Gruppe' angeben, wird das Attribut dieser Gruppe standardmäßig in allen Attributgruppen hinzugefügt:

$eavSetup->addAttribute('catalog_product', 'my_attribute', [
    ...
    'group' => 'General',  // or some other group name
    ...
]);

5

Dies könnte hilfreich sein: -

Erstellen Sie InstallData.php in Ihrem Setup-Ordner und fügen Sie diesen Code in Ihre Datei ein

<?php

namespace [Vendor]\[Module]\Setup;

use Magento\Eav\Setup\EavSetup; 
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{

    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory; 
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        /**
         * Add attributes to the eav/attribute
         */
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY,'background_image');
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'background_image',/* Custom Attribute Code */
            [

                'type' => 'varchar',/* Data type in which formate your value save in database*/
                'backend' => '',
                'frontend' => '',
                'label' => 'Background Image', /* lablel of your attribute*/
                'input' => 'media_image',
                'class' => '',
                'source' => '',
                                /* Source of your select type custom attribute options*/
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                                    /*Scope of your attribute */
                'visible' => false,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false
            ]
        );


        $entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
        $attributeSetIds = $eavSetup->getAllAttributeSetIds($entityTypeId);
        foreach ($attributeSetIds as $attributeSetId) {
            /**
             * getAttributeGroupId($entityTypeId, $attributeSetId, "Group_Code");
             * 
             */
            $groupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSetId, "image-management");
            $eavSetup->addAttributeToGroup(
                $entityTypeId,
                $attributeSetId,
                $groupId,
                'background_image',
                null
            );
        }
    }
}

1
Ich habe festgestellt, dass dies der einzige Weg ist, wenn Sie versuchen, der Gruppe "Bilder" ein Attribut hinzuzufügen. Magento löst Fehler aus, wenn Sie die Methode 'group' => '<groupenname>' verwenden.
dmatthew

3

Hier ist das Skript, mit dem Sie allen Attributgruppen Attribute hinzufügen können.
Schritt 1: -Erstellen Sie InstallData.php in Ihrem Setup-Ordner und fügen Sie diesen Code in Ihre Datei ein.

namespace Ktpl\BrandManagement\Setup;

    use Magento\Eav\Setup\EavSetup;
    use Magento\Eav\Setup\EavSetupFactory;
    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;

    class InstallData implements InstallDataInterface
    {
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
     public function install(ModuleDataSetupInterface $setup,     ModuleContextInterface $context)
    {
     /** @var EavSetup $eavSetup */
     $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

     /**
     * Add attributes to the eav/attribute
     */

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
            'shop_by_brand',
            [
                'type' => 'varchar',
                'backend' => '',
                'frontend' => '',
                'label' => 'Brand',
                'input' => 'multiselect',
                'class' => '',
                'source' => '',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => 0,
                'searchable' => false,
                'filterable' => true,
                'comparable' => false,
                'visible_on_front' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
     }
   }

Bitte lassen Sie mich wissen, wenn Sie Fragen dazu haben. Bitte entfernen Sie bei Bedarf zusätzlichen Platz.


Damit eine bestimmte Gruppe zugewiesen werden kann, müssen Sie diese Zeile 'group' => '<group_name>' hinzufügen
Manish Goswami
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.