Trong quá trình làm việc với Magento 2, chắc chắn sẽ có lúc bạn muốn thêm vào Product form một field phải không nào. Và Magento có hỗ trợ bạn làm điều đó. Trong bài viết này tôi sẽ hướng dẫn các bạn thêm product attribute trong Magento 2 với hai cách:
Cách 1: Thêm Product Attribute trong Magento 2 với Setup Script
Đầu tiên các bạn tạo file ViMagento/HelloWorld/Setup/InstallData.php nằm trong module ViMagento_HelloWorld với nội dung sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?php namespace ViMagento\HelloWorld\Setup; use Magento\Framework\Setup\UpgradeDataInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; class UpgradeData implements UpgradeDataInterface { private $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory) { $this->eavSetupFactory = $eavSetupFactory; } public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { if (version_compare($context->getVersion(), '1.0.2', '<')) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, 'use_setup_script', [ 'group' => 'General', 'attribute_set_id' => 'Bag', 'type' => 'varchar', 'backend' => '', 'frontend' => '', 'label' => 'Use Setup Script', 'input' => 'text', 'class' => '', 'source' => '', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 'visible' => true, 'required' => false, 'user_defined' => true, 'default' => 0, 'searchable' => true, 'filterable' => true, 'comparable' => true, 'visible_on_front' => true, 'sort_order' => 50, 'option' => [ 'values' => [], ] ] ); } } } |
Các attribute:
- group: các attribute được nhóm thành các group, giá trị này cho biết attribute sẽ thuộc về group cha nào.
- type: xác định attribute này sẽ được lưu vào bảng nào trong cơ sở dữ liệu.
- backend: xác định class sẽ thực hiện 1 số hành động cụ thể khi load hoặc save attribute.
- frontend: xác định cách hiển thị attribute.
- label: giá trị sẽ hiển thị ở cả frontend và backend.
- input: xác định kiểu đầu vào như select, text…
- source: danh sách các options được sử dụng cho kiểu đầu vào dạng select, multi select..
- global: xác định phạm vi của attribute global, website hay store.
- required: xác định liệu có bắt buộc nhập hay không.
- visible: xác định liệu attribute có được hiển thị ở cả backend và frontend hay không.
- visible_on_front: xác định liệu attribute có được hiển thị ở tab “More Information” trong trang product detail hay không. Và nó cũng phụ thuộc vào thuộc tính visible ở trên.
- default: đặt một giá trị mặc định cho attribute.
- is_used_in_grid: xác định liệu attribute có được sử dụng trong grid hay không.
- is_visible_in_grid: xác định liệu attribute có được hiển thị trong grid hay không.
- is_filterable_in_grid: xác định liệu attribute có được filter trong grid hay không.
- user_defined: xác định liệu admin có quyền xóa hoặc thay đổi attribute hay không.
- searchable: xác định liệu có thể sử dụng attribute để search hay không.
- unique: có là giá trị duy nhất hay không.
- option: xác định danh sách các option được sử dụng cho select, multi select..
- sort_order: thứ tự sắp xếp của attribute.
- apply_to: áp dụng cho loại product nào. Ví dụ: simple product, virtual product ….
Sau đó nâng version của module lên 1.0.2, chạy câu lệnh php bin/magento setup:upgrade và truy cập vào trang admin của bạn Catalog > Product > Add Product

Cách 2: Thêm Product Attribute trong Magento 2 với Data Patch
Bạn nào muốn tìm hiểu thêm về Data Patch trong Magento có tham khảo bài này. Sử dụng Data Patch trong Magento 2.
Tạo file: ViMagento/HelloWorld/Setup/Patch/Data/AddProductAttribute.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?php namespace ViMagento\HelloWorld\Setup\Patch\Data; use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; class AddProductAttribute implements DataPatchInterface { /** * ModuleDataSetupInterface * * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * EavSetupFactory * * @var EavSetupFactory */ private $eavSetupFactory; /** * AddProductAttribute constructor. * * @param ModuleDataSetupInterface $moduleDataSetup * @param EavSetupFactory $eavSetupFactory */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory ) { $this->moduleDataSetup = $moduleDataSetup; $this->eavSetupFactory = $eavSetupFactory; } /** * {@inheritdoc} */ public function apply() { /** @var EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); $eavSetup->addAttribute('catalog_product', 'datapatch', [ 'type' => 'int', 'label' => 'Use Data Patch', 'input' => 'text', 'default' => 0, 'global' => ScopedAttributeInterface::SCOPE_STORE, 'visible' => true, 'used_in_product_listing' => true, 'user_defined' => true, 'required' => false, 'group' => 'General', 'sort_order' => 80, ]); } /** * {@inheritdoc} */ public static function getDependencies() { return []; } /** * {@inheritdoc} */ public function getAliases() { return []; } public static function getVersion() { return '1.0.1'; } } |
Sau đó bạn cũng chạy php bin/magento setup:upgrade và truy cập vào trang admin của bạn Catalog > Product > Add Product

Kết luận
Trong bài viết tôi đã hướng dẫn cho các bạn về cách tạo product attribute trong Magento 2. Cám ơn các bạn đã đọc bài viết.
Em dùng magento 2 nhưng setup sripts này vào admin không hiển thị