データ同期中に製品属性を動的に追加

データの同期処理中に属性を追加するプラグインを作成することで、Adobe Commerceに属性を登録することなく、製品属性を拡張できます。

NOTE
製品属性を拡張する最適な方法は、Adobe Commerceに追加することです。この場合、Commerce管理者から設定および管理できます。 動的に追加するのは、Commerce ストアフロントサービス専用で必要であり、Adobe Commerceには登録しない場合のみです。 また、カタログサービスを使用した API メッシュを使用してカスタム属性を管理し、カタログサービスのGraphQL スキーマを拡張するオプションもあります。

製品属性の追加

customer_attribute クラスに Magento\CatalogDataExporter\Model\Provider\Product\Attributes を追加するプラグインを作成します。

  1. 依存関係挿入設定ファイルdi.xml)を更新して、プラグインを定義します。

    code language-xml
    <type name="Magento\CatalogDataExporter\Model\Provider\Product\Attributes">
      <plugin name="product_customer_attributes" type="Vendor\CatalogDataExporter\Model\Plugin\AddAttribute"/>
    </type>
    
  2. プラグインを作成します。

    code language-php
     <?php
     declare(strict_types=1);
    
     namespace Vendor\CatalogDataExporter\Model\Plugin;
    
     use Magento\CatalogDataExporter\Model\Provider\Product\Attributes;
    
     class AddAttribute {
    
          /**
           * @param Attributes $subject
           * @param array $result
           * @param $arguments
           * @return array
           * @throws \Zend_Db_Statement_Exception
           */
           public function afterGet(Attributes $subject, array $result, $arguments): array
           {
               $additionalAttributes = [];
               $attributeCode = 'customer_attribute';
               foreach ($result as $product) {
                   if (!isset($product['productId']) || !isset($product['storeViewCode'])) {
                       continue;
                   }
                   // HINT: if needed, do filtration by "storeViewCode" and or "productId"
    
                   $productId = $product['productId'];
                   $storeViewCode = $product['storeViewCode'];
    
                   $newKey = \implode('-', [$product['storeViewCode'], $product['productId'], $attributeCode]);
                   if (isset($additionalAttributes[$newKey])) {
                       continue;
                   }
                   $additionalAttributes[$newKey] = [
                       'productId' => $productId,
                       'storeViewCode' => $storeViewCode,
                       'attributes' => [
                           'attributeCode' => $attributeCode,
                           // provide single or multiple values for the attribute
                           'value' => [
                               rand(1, 42)
                           ]
                       ]
                   ];
    
               }
    
               return array_merge($result, $additionalAttributes);
           }
     }
    

    プラグインを追加すると、次回スケジュールされた同期中に、変更内容が接続されたストアフロントサービスに同期されます。 更新を直ちに送信するには、次の CLI コマンドを使用して同期プロセスを手動で開始します。

    code language-none
    bin/magento saas:resync --feed=products
    

カスタム製品属性メタデータの宣言

カスタムの製品属性を動的に作成し、それをストアフロントサービスでの表示、検索またはフィルタリングに使用する場合は、製品属性メタデータを追加して、ストアフロントの動作を設定します。

  1. 依存関係挿入設定ファイルdi.xml)を更新して、製品属性メタデータのプラグインを定義します。

    code language-xml
    <type name="\Magento\CatalogDataExporter\Model\Provider\ProductMetadata">
      <plugin name="product_customer_attributes_metadata" type="Vendor\CatalogDataExporter\Model\Plugin\AddAttributeMetadata"/>
    </type>
    
  2. 次のプロバイダー \Magento\CatalogDataExporter\Model\Provider\ProductMetadata にプラグインを作成します。

    必須フィ ProductAttributeMetadata ルドの vendor/magento/module-catalog-data-exporter/etc/et_schema.xml をチェックインします。

    code language-php
     <?php
     declare(strict_types=1);
    
     namespace Vendor\CatalogDataExporter\Model\Plugin;
    
     use Magento\CatalogDataExporter\Model\Provider\ProductMetadata;
    
     class AddAttributeMetadata {
    
          /**
           * @param ProductMetadata $subject
           * @param array $result
           * @param $arguments
           * @return array
           * @throws \Zend_Db_Statement_Exception
           */
          public function afterGet(ProductMetadata $subject, array $result, $arguments): array
          {
               $result[] = [
                 'id' => '123',
                 // provide storeCode, websiteCode and storeViewCode applicable for your AC instance
                 'storeCode' => 'default',
                 'websiteCode' => 'base',
                 'storeViewCode' => 'default',
                 // specify the customer attribute code - should be the same as used in the products attributes plugin
                 'attributeCode' => 'customer_attribute',
                 // only product attributes are supported
                 'attributeType' => 'catalog_product',
                 // specify the data type
                 'dataType' => 'int',
                 'multi' => false,
                 'label' => 'Customer Attribute',
                 'frontendInput' => 'select',
                 'required' => false,
                 'unique' => false,
                 'global' => true,
                 'visible' => true,
                 'searchable' => true,
                 'filterable' => true,
                 // This value must be set to true to export it to the storefront services
                 'visibleInCompareList' => true,
                 'visibleInListing' => true,
                 'sortable' => true,
                 'visibleInSearch' => true,
                 'filterableInSearch' => true,
                 'searchWeight' => 1.0,
                 'usedForRules' => true,
                 'boolean' => false,
                 'systemAttribute' => false,
                 'numeric' => false,
                 // specify the list of attribute options
                 'attributeOptions' => [
                     'option1',
                     'option2',
                     'option3'
                 ]
              ];
    
               return $result;
          }
       }
    

    プラグインを追加すると、次回スケジュールされた同期中に、変更内容が接続されたストアフロントサービスに同期されます。 更新を直ちに送信するには、次の CLI コマンドを使用して同期プロセスを手動で開始します。

    code language-none
    bin/magento saas:resync --feed=productattributes
    
recommendation-more-help
84c95778-e795-4ef1-8b7e-54d73e45e22d