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

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

  1. 依存関係挿入設定ファイルdi.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 をチェックインします。

     <?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',
                 '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 コマンドを使用して同期プロセスを手動で開始します。

    bin/magento saas:resync --feed=productattributes
    
recommendation-more-help