Cách tạo thuộc tính sản phẩm và gán cho nó tất cả thuộc tính được đặt trong magento 2


7

Magento 2 - Làm cách nào để tạo thuộc tính sản phẩm và gán cho tất cả các thuộc tính được đặt bằng tập lệnh thiết lập?

Câu trả lời:


10

Theo mặc định, nếu bạn cung cấp một giá trị cho 'nhóm' trong mảng cấu hình thuộc tính, thuộc tính sẽ được thêm vào nhóm đó trong tất cả các bộ thuộc tính:

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

5

Điều này có thể hữu ích: -

Tạo InstallData.php trong thư mục Cài đặt của bạn và dán mã này vào tệp của bạn

<?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
Tôi thấy rằng nếu bạn đang cố gắng thêm một thuộc tính vào nhóm 'Hình ảnh' thì đây là cách duy nhất. Magento ném lỗi khi bạn sử dụng phương thức 'nhóm' => '<tên nhóm>'.
dmatthew

3

Đây là kịch bản mà bạn có thể thêm thuộc tính cho tất cả các tập thuộc tính.
Bước 1: -Tạo InstallData.php trong thư mục Cài đặt của bạn và dán mã này vào tệp của bạn.

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' => ''
            ]
        );
     }
   }

Xin vui lòng cho tôi biết nếu bạn có bất kỳ câu hỏi về nó. vui lòng loại bỏ thêm không gian nếu có yêu cầu.


Để nhóm vuông góc được gán, bạn cần thêm dòng này 'nhóm' => '<group_name>'
Manish Goswami
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.