Modify a Product attribute in Magento 2

Modify a Product attribute in Magento 2

·

2 min read

So, today we are going to learn how we can modify the behavior of a product attribute based on any conditions.

Let's say we have an EAV attribute created through DataPatch or using the Magento admin panel. A client asks that he want to visible that attribute using system configuration settings.

First, create System configurations:

File-path: etc/adminhtml/system.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">

<group id="product_attributes" sortOrder="10" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
                <label>Allow Attributes</label>
                <field id="show_attribute_to_admin_form" type="select" sortOrder="10" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
                    <label>Visible attribute on Form</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
</config>

Now, we have already created the system configurations for our attribute. Based on its value the attribute will be visible on the product form.

Let's create our DataModifier class to manage the attribute visibility.

<?php

namespace Ejaz\Alam\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Customer\Model\Session;
use Magento\Store\Model\ScopeInterface;

class ProductAttribute extends AbstractModifier
{

    private $customerSession;

    private $sellerFactory;
    private \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig;

    public function __construct(
        Session $customerSession,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->customerSession = $customerSession;
        $this->sellerFactory = $sellerFactory;
        $this->scopeConfig = $scopeConfig;
    }

    public function modifyMeta(array $meta)
    {
        $customerId = $this->customerSession->getId();
        $status = $this->sellerFactory->create()->load($customerId, 'customer_id')->getStatus();

        if ($this->customerSession->isLoggedIn()
            && $status == 1
            && isset($meta['product-details']['children']['container_category_ids']['children']
                ['category_ids']['arguments']['data']['config'])) {
            $meta['product-details']['children']['container_category_ids']['children']
            ['category_ids']['arguments']['data']['config']['visible'] = 0;
            $meta['product-details']['children']['container_category_ids']['children']
            ['create_category_button']['arguments']['data']['config']['visible'] = 0;
        }
        return $meta;

    }
    private function isAllowToAddCategory()
    {
        return $this->scopeConfig->getValue('ejaz\sellers_product\show_attribute_to_admin_form', ScopeInterface::SCOPE_STORE);
    }

    /**
     * {@inheritdoc}
     */
    public function modifyData(array $data)
    {
        return $data;
    }
}

Our class for managing the visibility of the attribute is ready, now we will add our DataModifier into the Pool.

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool" type="Magento\Ui\DataProvider\Modifier\Pool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="attribute_price_comparison" xsi:type="array">
                    <item name="class" xsi:type="string">Ejaz\Alam\Ui\DataProvider\Product\Form\Modifie\ProductAttribute</item>
                    <item name="sortOrder" xsi:type="number">800</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

DI compile the code using

php bin/magento setup:di:compile

clear the cache and check the results.

if you got any question, write below in the comment section.