动态添加产品属性

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

NOTE
扩展产品属性的最佳方法是使用 Catalog Service🔗将这些属性添加到 Adobe Commerce where you can configure and manage them from the Commerce Admin. Only add them dynamically if you need them solely for Commerce storefront services and do not want to register them in Adobe Commerce. You also have the option to manage custom attributes using API Mesh 以扩展Catalog Service 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-shell
    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创建插件。

    检查vendor/magento/module-catalog-data-exporter/etc/et_schema.xml中的ProductAttributeMetadata以了解必填字段。

    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-shell
    bin/magento saas:resync --feed=productAttributes
    
recommendation-more-help
commerce-help-data-export