Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top