Wednesday 1 August 2018

Enable Price field of configurable product in admin in Magento 2.

As we can see Magento2 is much more depends on KO(Knockout JS).

Magento 2 don't allow to update price from admin for the configurable product.

Once configurable product is created from admin, price field is disabled.

To enable price field, I have created module and changed small code.

I don't know this is the proper method or not, but that is working for me.


1. Create a PHP file: app/code/Gopal/ConfigurablePrice/registration.php
 <?php  
 \Magento\Framework\Component\ComponentRegistrar::register(  
   \Magento\Framework\Component\ComponentRegistrar::MODULE,  
   'Gopal_ConfigurablePrice',  
   __DIR__  
 );  

2. Create a XML file: app/code/Gopal/ConfigurablePrice/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Gopal_ConfigurablePrice" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog" />
            <module name="Magento_ConfigurableProduct" />
        </sequence>
    </module>
</config>


3. Create a XML file: app/code/Gopal/ConfigurablePrice/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\ConfigurableProduct\Ui\DataProvider\Product\Form\Modifier\ConfigurablePrice">
        <plugin name="enable_price_configurable_product" type="Gopal\ConfigurablePrice\Plugin\DataProvider\Product\Form\Modifier\ConfigurablePrice" sortOrder="20" disabled="false"/>
    </type>
</config>


4. Create a file: app/code/Gopal/ConfigurablePrice/Plugin/DataProvider/Product/Form/Modifier/ConfigurablePrice.php
<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Gopal\ConfigurablePrice\Plugin\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableType;
use Magento\ConfigurableProduct\Ui\DataProvider\Product\Form\Modifier\ConfigurablePrice as ConfigurablePriceCore;
/**
 * Data provider for price in the Configurable products
 */
class ConfigurablePrice extends ConfigurablePriceCore
{
    /**
     * {@inheritdoc}
     *
     * Added after listener for make price field editable in configurable produc edit page.
     */
    public function afterModifyMeta($subject, $result)
    {
        $groupCode = $this->getGroupCodeByField($result, ProductAttributeInterface::CODE_PRICE)
            ?: $this->getGroupCodeByField($result, ConfigurablePriceCore::CODE_GROUP_PRICE);

        if (!empty($result[$groupCode]['children'][ConfigurablePriceCore::CODE_GROUP_PRICE])) {
            $result[$groupCode]['children'][ConfigurablePriceCore::CODE_GROUP_PRICE]['children']['price']['arguments']['data']['config']['imports']['disabled'] = 0;
        }

        return $result;
    }
}


Now, please run below commands
php bin/magento module:enable Gopal_ConfigurablePrice
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f


Now price field is must be enabled: Check screenshot of the configuable product page in admin.


If above link is useful to you then please accept answer at: https://magento.stackexchange.com/questions/171452/magento-2-edit-price-field-in-configurable-product-in-admin-panel



9 comments:

  1. This is not working in Magento 2.3

    ReplyDelete
  2. Fix for 2.3:

    Change:
    $result[$groupCode]['children'][ConfigurablePriceCore::CODE_GROUP_PRICE]['children']['price']['arguments']['data']['config']['imports']['disabled'] = 0;

    To:
    $result[$groupCode]['children'][ConfigurablePriceCore::CODE_GROUP_PRICE]['children']['price']['arguments']['data']['config']['component'] = 'Magento_Ui/js/form/element/abstract';

    This removed the custom component which is now in control of disabling the input.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Congratulations!

    its working for me.

    There is some sql query that allows me to batch update all my configurable products with the min / max price of simple products (children)

    ReplyDelete
  5. the price field is enabled using your solution and the price is saving in the database but the problem is the saved price is not coming in edit form.
    the price field always coming with value 0.
    any suggestion? how I fix this issue?

    ReplyDelete
  6. Does anyone know if this happens to be compatible with 2.4.x?

    ReplyDelete
  7. how add discount price (advanced price is hidden)

    ReplyDelete