在数据同步期间动态添加产品属性

您可以通过创建插件在数据同步过程中添加属性,来扩展产品属性,而无需在Adobe Commerce中注册这些属性。

NOTE
扩展产品属性的最佳方法是将这些属性添加到Adobe Commerce,您可以在其中通过Commerce管理员配置和管理这些属性。 仅当您仅用于Commerce店面服务并且不想在Adobe Commerce中注册时,才需要动态添加这些插件。 您还可以选择将API Mesh与目录服务结合使用来管理自定义属性,以扩展目录服务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