zoukankan      html  css  js  c++  java
  • Magento 2开发教程

    添加产品属性是一种在Magento 1 和 Magento 2最受欢迎的业务。 属性是解决许多与产品相关的实际任务的有力方法。

    这是一个相当广泛的话题,但在这个视频中,我们将讨论添加一个下拉类型属性到产品的简单过程。

    对于这个练习,假定安装了示例数据集。

    • 我们将添加一个属性叫做clothing_material与可能的值:Cotton, Leather, Silk, Denim, Fur, 和 Wool.
    • 我们将在“产品视图”页面上以粗体文本显示此属性。
    • 我们将它分配给默认属性集,并添加一个限制,任何“底部”的衣服,如休闲裤,不能是材料毛皮。

    我们需要采取以下步骤来添加新的属性:

    1. 创建新模块.
    2. 添加一个安装数据脚本。
    3. 添加源模型。
    4. 添加后端模型。
    5. 添加前端模型
    6. 执行安装数据脚本验证它的工作。

    让我们走过每一步。

    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.
     */
    MagentoFrameworkComponentComponentRegistrar::register(
        MagentoFrameworkComponentComponentRegistrar::MODULE,
        'Learning_ClothingMaterial',
        __DIR__
    );

    2:创建安装数据脚本

    接下来,我们需要创建安装数据脚本 因为在技术上添加属性将记录添加到多个表中,例如 eav_attributecatalog_eav_attribute, 这是数据操作,而不是模式更改。 因此,我们用installschema 和 installdata。

    创建文件 app/code/Learning/ClothingMaterial/Setup/InstallData.php:

    /**
     * Copyright © 2016 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */
    
    namespace LearningClothingMaterialSetup;
    
    use MagentoFrameworkSetupInstallDataInterface;
    use MagentoFrameworkSetupModuleContextInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    
    /**
    * @codeCoverageIgnore
    class InstallData implements InstallDataInterface
    {
        /**
         * Eav setup factory
         * @var EavSetupFactory
         */
        private $eavSetupFactory;
    
        /**
         * Init
         * @param CategorySetupFactory $categorySetupFactory
         */
        public function __construct(MagentoEavSetupEavSetupFactory $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(
                MagentoCatalogModelProduct::ENTITY,
                'clothing_material',
                [
                    'group' => 'General',
                    'type' => 'varchar',
                    'label' => 'Clothing Material',
                    'input' => 'select',
                    'source' => 'LearningClothingMaterialModelAttributeSourceMaterial',
                    'frontend' => 'LearningClothingMaterialModelAttributeFrontendMaterial',
                    'backend' => 'LearningClothingMaterialModelAttributeBackendMaterial',
                    'required' => false,
                    'sort_order' => 50,
                    'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::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_attributeeav_attribute 表。 注意,这些表中的字段与属性在addAttribute() 方法。

    要查看所有映射,您应该查看MagentoCatalogModelResourceModelSetupPropertyMapper 类.

    3: 添加资源模型

    接下来,我们需要创建资源模型:

    app/code/Learning/ClothingMaterial/Model/Attribute/Source/Material.php

    /**
     * Copyright © 2016 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */
    
    namespace LearningClothingMaterialModelAttributeSource;
    
    class Material extends MagentoEavModelEntityAttributeSourceAbstractSource
    {
        /**
         * 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 LearningClothingMaterialModelAttributeBackend;
    
    class Material extends MagentoEavModelEntityAttributeBackendAbstractBackend
    {
        /**
         * Validate
         * @param MagentoCatalogModelProduct $object
         * @throws MagentoFrameworkExceptionLocalizedException
         * @return bool
         */
        public function validate($object)
        {
            $value = $object->getData($this->getAttribute()->getAttributeCode());
            if ( ($object->getAttributeSetId() == 10) && ($value == 'wool')) {
                throw new MagentoFrameworkExceptionLocalizedException(
                    __('Bottom can not be wool.')
                );
            }
            return true;
        }
    }


    5: 添加一个前端模型

    namespace LearningClothingMaterialModelAttributeFrontend;
    
    class Material extends MagentoEavModelEntityAttributeFrontendAbstractFrontend
    {
        public function getValue(MagentoFrameworkDataObject $object)
        {
            $value = $object->getData($this->getAttribute()->getAttributeCode());
            return "<b>$value</b>";
        }
    }


    与后端模型一样,这也是一个非常简单的类。

    6: 执行installdata脚本验证它的工作

    现在我们可以运行我们的代码和检查结果:

    $ cd <magento2_root>
    $ php bin/magento setup:upgrade
    

    运行此之后,新属性应该已添加到数据库中。 您可以检查 eav_attributecatalog_eav_attribute 表来验证属性及其属性是否存在。

    查看原文

  • 相关阅读:
    JeePlus:代码生成器
    JeePlus:API工具
    Java实现 洛谷 P1023 税收与补贴问题
    Java实现 洛谷 P1023 税收与补贴问题
    Java实现 洛谷 P1023 税收与补贴问题
    Java实现 洛谷 P1328 生活大爆炸版石头剪刀布
    Java实现 洛谷 P1328 生活大爆炸版石头剪刀布
    Java实现 洛谷 P1328 生活大爆炸版石头剪刀布
    Java实现 洛谷 P1328 生活大爆炸版石头剪刀布
    Java实现 洛谷 P1328 生活大爆炸版石头剪刀布
  • 原文地址:https://www.cnblogs.com/xz-src/p/6920359.html
Copyright © 2011-2022 走看看