Magento 2開發教程 - 如何添加新產品屬性
添加產品屬性是一種在Magento 1 和 Magento 2最受歡迎的業務。 屬性是解決許多與產品相關的實際任務的有力方法。
這是一個相當廣泛的話題,但在這個視頻中,我們將討論添加一個下拉類型屬性到產品的簡單過程。
對於這個練習,假定安裝了示例數據集。
- 我們將添加一個屬性叫做clothing_material與可能的值:Cotton, Leather, Silk, Denim, Fur, 和 Wool.
- 我們將在“產品視圖”頁面上以粗體文本顯示此屬性。
- 我們將它分配給默認屬性集,並添加一個限制,任何“底部”的衣服,如休閑褲,不能是材料毛皮。
我們需要采取以下步驟來添加新的屬性:
- 創建新模塊.
- 添加一個安裝數據腳本。
- 添加源模型。
- 添加後端模型。
- 添加前端模型
- 執行安裝數據腳本驗證它的工作。
讓我們走過每一步。
1:創建新模塊
如Magento是模塊化的,我們通過創建一個新的模塊稱為啟動過程Learning_ClothingMaterial
.
$ cd <magento2_root>/app/code
$ mkdir Learning
$ mkdir Learning/ClothingMaterial
現在,創建兩個文件:
etc/module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright ? 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Learning_ClothingMaterial" setup_version="0.0.1">
</module>
</config>
registration.php
/**
* Copyright ? 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
‘Learning_ClothingMaterial‘,
__DIR__
);
2:創建安裝數據腳本
接下來,我們需要創建安裝數據腳本 因為在技術上添加屬性將記錄添加到多個表中,例如 eav_attribute
和 catalog_eav_attribute,
這是數據操作,而不是模式更改。 因此,我們用installschema 和 installdata。
創建文件 app/code/Learning/ClothingMaterial/Setup/InstallData.php
:
/**
* Copyright ? 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Learning\ClothingMaterial\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* @codeCoverageIgnore
class InstallData implements InstallDataInterface
{
/**
* Eav setup factory
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
* @param CategorySetupFactory $categorySetupFactory
*/
public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* [email protected]}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create();
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
‘clothing_material‘,
[
‘group‘ => ‘General‘,
‘type‘ => ‘varchar‘,
‘label‘ => ‘Clothing Material‘,
‘input‘ => ‘select‘,
‘source‘ => ‘Learning\ClothingMaterial\Model\Attribute\Source\Material‘,
‘frontend‘ => ‘Learning\ClothingMaterial\Model\Attribute\Frontend\Material‘,
‘backend‘ => ‘Learning\ClothingMaterial\Model\Attribute\Backend\Material‘,
‘required‘ => false,
‘sort_order‘ => 50,
‘global‘ => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
‘is_used_in_grid‘ => false,
‘is_visible_in_grid‘ => false,
‘is_filterable_in_grid‘ => false,
‘visible‘ => true,
‘is_html_allowed_on_front‘ => true,
‘visible_on_front‘ => true
]
);
}
}
讓我們花點時間看看代碼。
首先,我們需要使用一個特殊的設置對象,而不是作為參數的對象。 這是因為目錄是一個EAV的實體,所以添加一個屬性,我們要用eavsetup而不是標準。 這也適用於在Magento 2任何EAV實體(類,產品,客戶,等等)。
這就是為什麽我們說在構造函數eavsetupfactory。
在 install()
方法, 我們所要做的就是給 addAttribute()
方法3個參數,實體類型、屬性代碼和屬性。
這些屬性定義屬性的行為。
可以看到一個完整的屬性列表 catalog_eav_attribute
和 eav_attribute
表。
註意,這些表中的字段與屬性在addAttribute()
方法。
要查看所有映射,您應該查看\Magento\Catalog\Model\ResourceModel\Setup\PropertyMapper
類.
3: 添加資源模型
接下來,我們需要創建資源模型:
app/code/Learning/ClothingMaterial/Model/Attribute/Source/Material.php
/**
* Copyright ? 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Learning\ClothingMaterial\Model\Attribute\Source;
class Material extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
/**
* Get all options
* @return array
*/
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = [
[‘label‘ => __(‘Cotton‘), ‘value‘ => ‘cotton‘],
[‘label‘ => __(‘Leather‘), ‘value‘ => ‘leather‘],
[‘label‘ => __(‘Silk‘), ‘value‘ => ‘silk‘],
[‘label‘ => __(‘Denim‘), ‘value‘ => ‘denim‘],
[‘label‘ => __(‘Fur‘), ‘value‘ => ‘fur‘],
[‘label‘ => __(‘Wool‘), ‘value‘ => ‘wool‘],
];
}
return $this->_options;
}
}
顧名思義,是 getAllOptions
方法提供所有可用選項的列表。
4: 添加後端模型
app/code/Learning/ClothingMaterial/Model/Attribute/Backend/Material.php
/**
* Copyright ? 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Learning\ClothingMaterial\Model\Attribute\Backend;
class Material extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
{
/**
* Validate
* @param \Magento\Catalog\Model\Product $object
* @throws \Magento\Framework\Exception\LocalizedException
* @return bool
*/
public function validate($object)
{
$value = $object->getData($this->getAttribute()->getAttributeCode());
if ( ($object->getAttributeSetId() == 10) && ($value == ‘wool‘)) {
throw new \Magento\Framework\Exception\LocalizedException(
__(‘Bottom can not be wool.‘)
);
}
return true;
}
}
5: 添加一個前端模型
namespace Learning\ClothingMaterial\Model\Attribute\Frontend;
class Material extends \Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend
{
public function getValue(\Magento\Framework\DataObject $object)
{
$value = $object->getData($this->getAttribute()->getAttributeCode());
return "<b>$value</b>";
}
}
與後端模型一樣,這也是一個非常簡單的類。
6: 執行installdata腳本驗證它的工作
現在我們可以運行我們的代碼和檢查結果:
$ cd <magento2_root>
$ php bin/magento setup:upgrade
運行此之後,新屬性應該已添加到數據庫中。 您可以檢查 eav_attribute
和 catalog_eav_attribute
表來驗證屬性及其屬性是否存在。
查看原文
Magento 2開發教程 - 如何添加新產品屬性