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;
}
/**
* {@inheritdoc}
* @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开发教程 - 如何添加新产品属性的更多相关文章
- Magento入门开发教程
Modules->模块 Controller->控制器 Model->模型 Magento是这个星球上最强大的购物车网店平台.当然,你应该已经对此毫无疑问了.不过,你可能还不知道,M ...
- Magento 2开发教程 - 创建新模块
视频在youtube网站国内访问不了,可以使用FQ软件查看. 视频地址:www.youtube.com/embed/682p52tFcmY@autoplay=1 下面是视频文字介绍: Magento ...
- ios开发--给应用添加新的字体的方法
1.网上搜索字体文件(后缀名为.ttf,或.odf) 2.把字体库导入到工程的resouce中 3.在程序添加以下代码 输出所有字体 NSArray *familyNames = [UIFont fa ...
- 【Android 开发教程】动态添加Fragments
本章节翻译自<Beginning-Android-4-Application-Development>,如有翻译不当的地方,敬请指出. 原书购买地址http://www.amazon.co ...
- Android开发教程大全介绍
Android是由谷歌在2007年推出的一个开放系统平台,主要针对移动设备市场,目前版本为Android 4.0.Android基于Linux,开发者可以使用Java或C/C++开发Android应用 ...
- [转]Magento2开发教程 - 如何向数据库添加新表
本文转自:https://www.cnblogs.com/xz-src/p/6920365.html Magento 2具有特殊的机制,允许你创建数据库表,修改现有的,甚至添加一些数据到他们(如安装数 ...
- Magento2开发教程 - 如何向数据库添加新表
Magento 2具有特殊的机制,允许你创建数据库表,修改现有的,甚至添加一些数据到他们(如安装数据,已被添加在模块安装). 这种机制允许这些变化可以在不同的设备之间传输. 关键的概念是,而不是做你能 ...
- Drupal8开发教程:模块开发——创建新页面
之前我们已经通过<Drupal8开发教程:认识.info.yml文件>对模块的YAML文件有了了解,今天我们来看如何通过模块开发的方式添加一个新的页面. 在 Drupal 7 中,通过模块 ...
- 《ArcGIS Engine+C#实例开发教程》第二讲 菜单的添加及其实现
原文:<ArcGIS Engine+C#实例开发教程>第二讲 菜单的添加及其实现 摘要:在上一讲中,我们实现了应用程序基本框架,其中有个小错误,在此先跟大家说明下.在“属性”选项卡中,我们 ...
随机推荐
- jenkins+docker+docker machine 远程部署
dotnet publish -c Release docker build -t microtest:1.0 --build-arg microport=1000 -f "$WORKSPA ...
- leetcode 19. 删除链表的倒数第N个节点 JAVA
题目: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链 ...
- Mysql表操作《一》表的增删改查
一.表介绍 表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段 id,name,qq,age称为字段,其余的,一行内容称为一条记录 二.创建表 语法 ...
- [USACO17DEC]Standing Out from the Herd(广义后缀自动机)
题意 定义一个字符串的「独特值」为只属于该字符串的本质不同的非空子串的个数.如 "amy" 与 “tommy” 两个串,只属于 "amy" 的本质不同的子串为 ...
- Ubuntu 16.04.5安装docker
一:安装Ubuntu 16.04.5 下载地址: 1.magnet:?xt=urn:btih:C3C5FE05C329AE51C6ECA464F6B30BA0A457B2CA 2.http://m ...
- 【redis基础】
基础知识 http://www.runoob.com/redis/redis-partitioning.html redis性能分析 https://www.cnblogs.com/mushroom/ ...
- 百度地图API —— 制作多途经点的线路导航
[百度地图API]如何制作多途经点的线路导航——驾车篇 摘要: 休假结束,酸奶小妹要从重庆驾车去北京.可是途中要去西安奶奶家拿牛奶饼干呢!用百度地图API,能不能帮我实现这个愿望呢? ------ ...
- <b>与<strong> <em>与<i>标签的区别
<b>与 <strong>用在网页上都能使字体加粗,二者的不同是:<b>是物理元素 ;<strong>是逻辑元素. 物理元素强调的是一种物理行为.比如说 ...
- 【python】10分钟教你用python如何正确把妹
前言 今天没妹子约,刚好研究一下.如何用神奇的python打造一个把妹神器吧.看完这个,你们就能走向人生巅峰,迎娶白富美啦. 我知道你们想看看效果 image 当然啦,这只是测试版的效果,真正的版本可 ...
- fdisk 和 parted 分区工具
fdisk 和 parted: fdisk 是用来对 Linux 下的 MBR 分区进行操作的一款分区工具, 由于 MBR 的设计缺陷导致 MBR 不能处理大于 2TB 的硬盘, 并且主分区个数不能超 ...