Magento 2 admin product edit page skip few attributes loading

By | December 27, 2024
Spread the love

To optimize the Magento 2 Admin Product Edit page by skipping the loading of certain attributes, you can follow these steps:

1. Create a Custom Module

Ensure you have a custom module where you can safely extend Magento functionality.


2. Override the Attribute Load Logic

Magento 2 uses data providers to load attribute data for the product edit page. You can customize this behavior by overriding the product form’s data provider.

  1. Define a Plugin for the Data Provider: Create a plugin for Magento\Catalog\Ui\DataProvider\Product\Form\ProductDataProvider. This class is responsible for loading product data and attributes.
  2. Plugin Implementation: In your module’s di.xml, define a plugin:
    <type name="Magento\Catalog\Ui\DataProvider\Product\Form\ProductDataProvider">
        <plugin name="custom_skip_attributes_plugin" type="Vendor\Module\Plugin\ProductDataProviderPlugin" />
    </type>
  3. Create the Plugin Class: In Vendor\Module\Plugin\ProductDataProviderPlugin.php:
    namespace Vendor\Module\Plugin;
    
    class ProductDataProviderPlugin
    {
        /**
         * Modify the data by skipping specific attributes.
         *
         * @param \Magento\Catalog\Ui\DataProvider\Product\Form\ProductDataProvider $subject
         * @param array $result
         * @return array
         */
        public function afterGetData(
            \Magento\Catalog\Ui\DataProvider\Product\Form\ProductDataProvider $subject,
            array $result
        ) {
            $attributesToSkip = ['attribute_code_1', 'attribute_code_2']; // Add the attribute codes you want to skip
            
            foreach ($result as &$data) {
                if (isset($data['attributes'])) {
                    foreach ($attributesToSkip as $attributeCode) {
                        if (isset($data['attributes'][$attributeCode])) {
                            unset($data['attributes'][$attributeCode]);
                        }
                    }
                }
            }
            
            return $result;
        }
    }

3. Test Your Changes

  • Clear the cache: php bin/magento cache:flush
  • Reload the Admin Product Edit page to ensure the specified attributes are not loaded.

4. Additional Optimization

If the attribute data comes from specific UI components, you can customize the ui_component XML file to exclude those attributes by modifying or overriding the field configuration.

For example, in view/adminhtml/ui_component/product_form.xml:

<field name="attribute_code_1" remove="true" />
<field name="attribute_code_2" remove="true" />

This will entirely remove those fields from the form.

5. Debugging and Logging

To verify the skipped attributes, you can log the data array:

\Magento\Framework\App\ObjectManager::getInstance()
    ->get(\Psr\Log\LoggerInterface::class)
    ->debug(print_r($result, true));

This approach ensures you load only the necessary attributes, improving the performance of the Admin Product Edit page.