動態新增產品屬性
您可以在資料同步處理過程中建立外掛程式來新增屬性,而不需在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類別的外掛程式。
-
更新相依性插入組態檔 (
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> -
建立外掛程式。
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
宣告自訂產品屬性中繼資料
如果您動態建立自訂產品屬性,並想要將其用於店面服務中的顯示、搜尋或篩選,請新增產品屬性中繼資料以設定店面行為。
-
更新相依性插入組態檔 (
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> -
為下列提供者
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