Aggiungere dinamicamente attributi di prodotto

È possibile estendere gli attributi del prodotto senza registrarli in Adobe Commerce creando un plug-in per aggiungere gli attributi durante il processo di sincronizzazione dei dati.

NOTE
Il modo migliore per estendere gli attributi del prodotto è aggiungerli a 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 con Catalog Service per estendere lo schema Catalog Service GraphQL.

Aggiungi attributi prodotto

Creare un plug-in che aggiunga customer_attribute alla classe Magento\CatalogDataExporter\Model\Provider\Product\Attributes.

  1. Aggiorna il file di configurazione dependency injection (di.xml) per definire il plug-in.

    code language-xml
    <type name="Magento\CatalogDataExporter\Model\Provider\Product\Attributes">
      <plugin name="product_customer_attributes" type="Vendor\CatalogDataExporter\Model\Plugin\AddAttribute"/>
    </type>
    
  2. Crea il plug-in.

    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);
           }
     }
    

    Dopo aver aggiunto il plug-in, le modifiche vengono sincronizzate con i servizi vetrina connessi durante la successiva sincronizzazione pianificata. Per inviare immediatamente gli aggiornamenti, utilizzare il seguente comando CLI per avviare manualmente il processo di sincronizzazione.

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

Dichiarare metadati di attributi di prodotto personalizzati

Se crei dinamicamente un attributo di prodotto personalizzato e desideri utilizzarlo per la visualizzazione, la ricerca o il filtro nei servizi di vetrina, aggiungi i metadati dell’attributo di prodotto per configurare il comportamento della vetrina.

  1. Aggiornare il file di configurazione dependency injection (di.xml) per definire il plug-in per i metadati dell’attributo del prodotto.

    code language-xml
    <type name="Magento\CatalogDataExporter\Model\Provider\ProductMetadata">
      <plugin name="product_customer_attributes_metadata" type="Vendor\CatalogDataExporter\Model\Plugin\AddAttributeMetadata"/>
    </type>
    
  2. Creare il plug-in per il provider Magento\CatalogDataExporter\Model\Provider\ProductMetadata seguente.

    Controlla ProductAttributeMetadata in vendor/magento/module-catalog-data-exporter/etc/et_schema.xml per i campi obbligatori.

    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;
          }
       }
    

    Dopo aver aggiunto il plug-in, le modifiche vengono sincronizzate con i servizi vetrina connessi durante la successiva sincronizzazione pianificata. Per inviare immediatamente gli aggiornamenti, utilizzare il seguente comando CLI per avviare manualmente il processo di sincronizzazione.

    code language-shell
    bin/magento saas:resync --feed=productAttributes
    
recommendation-more-help
commerce-help-data-export