1

I am trying to add custom text field in product edit form in admin panel.

I have used below code in etc/adminhtml/di.xml

   <virtualType 
   name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
    <arguments>
        <argument name="modifiers" xsi:type="array">
            <item name="customTab" xsi:type="array">
                <item name="class" xsi:type="string">
                    Vendor\Module\Ui\DataProvider\Product\Form\Modifier\NewField
                </item>
                <item name="sortOrder" xsi:type="number">70</item>
            </item>
        </argument>
    </arguments>
</virtualType>

And used below code in Newfield.php

 <?php
 namespace Vendor\Module\Ui\DataProvider\Product\Form\Modifier;
 use Magento\Catalog\Model\Locator\LocatorInterface;
 use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
 use Magento\Ui\Component\Form\Fieldset;
 use Magento\Ui\Component\Form\Field;
 use Magento\Ui\Component\Form\Element\Select;
 use Magento\Ui\Component\Form\Element\DataType\Text;
 class NewField extends AbstractModifier
{
private $locator;
protected $_coreRegistry;
protected $_resource;
private $stockRegistry;
public function __construct(
    LocatorInterface $locator,
    \Magento\Framework\Registry  $coreRegistry,
    \Magento\Framework\App\ResourceConnection $resource,
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
) {
    $this->locator = $locator;
    $this->_coreRegistry = $coreRegistry;
    $this->_resource = $resource;
    $this->stockRegistry = $stockRegistry;
}
public function modifyData(array $data)
{
    return $data;
}

public function modifyMeta(array $meta)
{
        $meta = array_replace_recursive(
            $meta,
            [
                'custom_fieldset' => [
                    'arguments' => [
                        'data' => [
                            'config' => [
                                'label' => __('Actual Quantity'),
                                'componentType' => Fieldset::NAME,
                                'dataScope' => 'data.product.custom_fieldset',
                                'collapsible' => false,
                                'sortOrder' => 5,
                            ],
                        ],
                    ],
                    'children' => [
                    'custom_field' => $this->getCustomField()
                    ],
                ]
            ]
        );
        return $meta;
    }

public function getCustomField()
{
    return [
        'arguments' => [
            'data' => [
                'config' => [
                    'label' => __('Actual Qty'),
                    'componentType' => Field::NAME,
                    'formElement' => \Magento\Ui\Component\Form\Element\Input::NAME,
                    'dataScope' => 'enabled',
                    'dataType' => Text::NAME,
                    'sortOrder' => 10,
                    'value' => $this->getProductData(),
                ],
            ],
        ],
    ];
}

public function getConnection()
{
    $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    return $connection;
}
public function getProductData()
{
    $product = $this->_coreRegistry->registry('product');
    $productId = $product->getId();
    $connection = $this->getConnection();
    $sql = "select SUM(a.qty) as qty from quote_item as a join quote as b ON a.quote_id = b.entity_id 
                    where b.reserved_order_id is NULL and b.is_active = '1' and a.product_id = '".$productId."'";
    $resultSets = $connection->fetchAll($sql);
    if(isset($resultSets[0]['qty'])){
        $actualQty = $resultSets[0]['qty'];
        return $actualQty;
    }else{
        $productStock = $this->stockRegistry->getStockItem($productId);
        $productQty = $productStock->getQty();
        return $productQty;
       }

    }

 }

The text field is created in product edit form but i am looking for code how to move it after the Qty field. Now it has been created with separate field set.

Is it possible to move to after the qty field? Or is there any other methods to do that? Please anybody help me with this. Thanks

  • Why are you not doing this from product attribute which you can create from admin? – Ramkishan Suthar Nov 9 '18 at 6:57
  • Yes, but the value we need to show on the fly, if attribute created value we need to pass manually? – jafar pinjar Nov 9 '18 at 7:00
  • Ok it means you want that field value should be pre-filled. Is it so? – Ramkishan Suthar Nov 9 '18 at 7:02
  • yes, the value is returned from the function i written there getProductData(), That is also coming now, but only thing that has to move inside product detail tab, instead of creating new fieldset – jafar pinjar Nov 9 '18 at 7:03
  • You should create new attribute from admin and add this in attribute set. Then you should implement login in modifier class as you are doing. – Ramkishan Suthar Nov 9 '18 at 7:06
2

You can set default value from below code. Update modifyMeta function from like this.

public function __construct(
    \Magento\Framework\Stdlib\ArrayManager $arrayManager
) {
    $this->arrayManager = $arrayManager;
}

public function modifyMeta(array $meta)
{
    $fieldCode = 'custom_fieldset';
    $elementPath = $this->arrayManager->findPath($fieldCode, $meta, null, 'children');
    $containerPath = $this->arrayManager->findPath(static::CONTAINER_PREFIX . $fieldCode, $meta, null, 'children');
    if (!$elementPath) {
        return $meta;
    }

    $meta = $this->arrayManager->merge(
        $containerPath,
        $meta,
        [
            'children'  => [
                $fieldCode => [
                    'arguments' => [
                        'data' => [
                            'config' => [
                                'required'  => false,
                                'default'  => $this->getProductData()
                            ],
                        ],
                    ],
                ]
            ]
        ]
    );
 return $meta;
}

Let me know if you face any issue while doing this.

  • Hello Ramkishan, what is ArrayManager? which class i need to use to construct? this one use Magento\Framework\Stdlib\ArrayManager;? – jafar pinjar Nov 9 '18 at 8:34
  • I updated my answer please check. – Ramkishan Suthar Nov 9 '18 at 8:38
  • Yes @Ramkishan, used your code , but my field is disappeared from product view form. – jafar pinjar Nov 9 '18 at 8:42
  • product view form? – Ramkishan Suthar Nov 9 '18 at 8:46
  • Edit form, like when i open the product in admin panel, I can't see the new field over there, – jafar pinjar Nov 9 '18 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.