ブログ

Magento 2の移行コスト:eコマースストアをMagento2に移動します

Magento2-category-attributes

このブログでは、簡単なモジュールを使用して、プログラムでカスタムMagento2カテゴリ属性を作成する方法を学習します。 Magento2カテゴリの単純なテキストフィールド属性を作成しました。

すぐに達成できるスニペットは次のとおりです。モジュールについては、以下のコードに従う必要があります。

プログラムでMagento2カテゴリ属性を追加するにはどうすればよいですか?

デフォルトでは、Magentoは管理者からカテゴリ属性を追加する機能を提供していません。ただし、プログラムでカスタムカテゴリ属性を作成できます。以下で説明するこれらの手順に従うことで、カテゴリ属性を作成できます。

Magento 2でカスタムカテゴリ属性を作成する簡単なモジュールから始めましょう。次に、registration.phpファイルとmodule.xmlファイルを作成してモジュールを定義します。したがって、私はWebepowerをPackagenameとして使用しました。ここで、CustomAttributeはモジュール名です。

パス: app / code / Webepower / CustomAttribute / registration.php

<?php

Magento \ Framework \ Component \ ComponentRegistrar :: register(\ Magento \ Framework \ Component \ ComponentRegistrar :: MODULE、
    'Webepower_CustomAttribute'、
    __DIR__
);

ファイルmodule.xmlを作成します

Module.xmlは、実際にはMagento_Catelogモジュールに依存しています。

パス: app / code / Webepower / CustomAttribute / etc / module.xml

<?xml version = "1.0"?>

< config xmlns:xsi = "https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "urn:magento:framework:Module / etc / module.xsd">

    < module name = "Webepower_CustomAttribute" setup_version = "1.0.0" />

< / config>

ファイルInstallData.phpを作成します

次に、InstallData.phpを作成して、カスタムカテゴリ属性を定義する必要があります。

パス: app / code / Webepower / CustomAttribute / Setup / InstallData.php

<?php

名前空間Webepower \ CustomAttribute \ Setup;
Magento \ Framework \ Setup \ ModuleDataSetupInterfaceを使用します;
Magento \ Framework \ Setup \ ModuleContextInterfaceを使用します;
Magento \ Eav \ Setup \ EavSetupFactoryを使用します;
Magento \ Framework \ Setup \ UpgradeDataInterfaceを使用します;
クラスUpgradeDataはUpgradeDataInterfaceを実装します
{{
    プライベート$ eavSetupFactory;

    / **
     *コンストラクター
     *
     * @param \ Magento \ Eav \ Setup \ EavSetupFactory $ eavSetupFactory
     * /
     パブリック関数__construct(
        EavSetupFactory $ eavSetupFactory
    ){
        $ this-> eavSetupFactory = $ eavSetupFactory;
    }

    パブリック関数のアップグレード(
        ModuleDataSetupInterface $ setup、
        ModuleContextInterface $ context
    )。
    {{
        $ eavSetup = $ this-> eavSetupFactory-> create(['setup' => $ setup]);
        $ eavSetup-> addAttribute(
            \ Magento \ Catalog \ Model \ Category :: ENTITY、
            'category_custom_attribute'、
            [
                'type' => '文章'、
                'ラベル' => 'カテゴリカスタム属性'、
                '入力' => '文章'、
                'sort_order' => 100、
                'ソース' => ''、
                'グローバル' => \ Magento \ Eav \ Model \ Entity \ Attribute \ ScopedAttributeInterface :: SCOPE_STORE、
                '可視' =>本当、
                '必須' => false、
                'user_defined' =>本当、
                'システム' => false、
                'デフォルト' =>ヌル、
                'グループ' => '一般情報'、
                'バックエンド' => ''、
                'is_visible' =>本当、
            ]
            );
    }
}

category_form.xmlでカスタム属性フィールドを定義します

パス: app / code / Webepower / CustomAttribute / view / adminhtml / ui_component / category_form.xml

<?xml version = "1.0"?>

< form xmlns:xsi = "https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "urn:magento:module:Magento_Ui:etc / ui_configuration.xsd">
    < fieldset name = "general">
        < field name = "category_custom_attribute">
            <引数name = "data" xsi:type = "array">
                < item name = "config" xsi:type = "array">
                    < item name = "required" xsi:type = "boolean"> false< / item>
                    < item name = "validation" xsi:type = "array">
                        < item name = "required-entry" xsi:type = "boolean"> false< / item>
                    < / item>
                    < item name = "sortOrder" xsi:type = "number"> 333< / item>
                    < item name = "dataType" xsi:type = "string"> string< / item>
                    < item name = "formElement" xsi:type = "string"> input< / item>
                    < item name = "label" translate = "true" xsi:type = "string"> Category Custom Attributee< / item>
                < / item>
            < /引数>
        < / field>
    < / fieldset>
< / form>

続きを読む:- Magento 2移行コスト:eコマースストアをMagento2に移動します

これで、フィールドセットにカスタムカテゴリ属性を設定して、カテゴリページの[一般]グループに属性を表示しました。次のSetupUpgradeコマンドを実行して、モジュールをMagento2にインストールします。

MagentoをインストールしたMagentoインスタンスに移動し、コマンドラインを開きます。

 php bin / magento setup:upgrade
php bin / magento cache:flush
php bin / magento indexer:reindex

これで、ロケーションカタログ->に移動すると、バックエンドでカスタムカテゴリ属性を確認できます。カテゴリページ。いずれかのカテゴリをクリックすると、一般セクションのカスタムカテゴリ属性が表示されます。

ハッピーリーディング!