mediarox/module-catalog-attribute-setup

包含一个替代属性设置类,该类包括优化的创建/更新机制。

安装: 237

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:magento2-module

0.0.6 2022-09-14 13:12 UTC

This package is auto-updated.

Last update: 2024-09-16 11:42:43 UTC


README

描述

一个包含替代属性设置类的 Magento 2 模块/library。它的目的是简化产品分类属性的创建/更新。

安装

composer require mediarox/module-catalog-attribute-setup
bin/magento setup:upgrade

使用

在大多数使用场景中,我们“使用”提供的设置类

use Catalog\AttributeSetup\Setup\AttributeSetup;

或更具体地说

use Catalog\AttributeSetup\Setup\AttributeSetupFactory;

在补丁文件(周期性或数据补丁)中。

我们的目标是让每个想要安装/更新属性的补丁只需要提供一个信息数组。其余的由设置类处理。

为了更有效地说明这一点,请注意以下示例。

示例 1 - 周期性数据补丁

使用 周期性数据补丁 强制设置产品属性(对开发/未发布项目很有用)。

<your_module>/Setup/RecurringData.php

use Magento\Catalog\Model\Product;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Catalog\AttributeSetup\Setup\AttributeSetup;
use Catalog\AttributeSetup\Setup\AttributeSetupFactory;

class RecurringData implements InstallDataInterface
{
    protected AttributeSetupFactory $attributeSetup;

    public function __construct(
        AttributeSetupFactory $attributeSetup
    ) {
        $this->attributeSetup = $attributeSetup;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var AttributeSetup $attributeSetup */
        $attributeSetup = $this->attributeSetup->create([
            'setup' => $setup,
            'attributeData' => $this->getAttributes()
        ]);

        $setup->startSetup();
        $attributeSetup->addUpdateAttributes();
        $setup->endSetup();
    }

    public function getAttributes() : array
    {
        return [
            Product::ENTITY => [
                'manufacturer' => [
                    'is_filterable' => 0
                ] 
            ]
        ];
    }
}
示例解释
  1. 我们始终使用“getAttributes()”方法内部提供属性信息。您可以根据需要进行不同的处理。
  2. 周期性数据补丁使用“install”方法作为主要入口点。(在数据补丁中,使用“apply”方法)。
  3. 周期性数据补丁在每次“bin/magento setup:upgrade”时执行。如果您只想运行一次,请使用 数据补丁
  4. 与往常一样,在 Magento 中,我们仍然使用一个工厂类来创建新实例。(AttributeSetupFactory)
  5. 在“install”方法中,我们首先通过内部方法“create”创建新实例,并立即传递我们的属性信息。
    $attributeSetup = $this->attributeSetup->create([
    'setup' => $setup,
    'attributeData' => $this->getAttributes()
    ]);
  6. 然后,我们在实例上运行“addUpdateAttributes”方法以启动创建/更新过程。
     $attributeSetup->addUpdateAttributes();

附加说明(是的,很重要)

属性信息结构

如示例所示,属性数组被分组到相应的实体中。('catalog_product' 或 'catalog_category')

属性属性名

不幸的是,Magento 没有使用统一的名称来表示属性属性。在某些情况下,您必须使用短形式,在其他情况下使用长形式。因此,我们 决定 统一使用 长形式。在此之后,该模块将加载模块 eav-property-mapper 作为依赖项,以确保我们可以/必须在整个系统中使用长形式。(所有短形式和长形式

总之:必须使用长形式。如果不这样做,脚本将因验证而中止。