2

I have used below code to change the value of custom attribute when the product is viewed in admin.

<?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">
<arguments>
    <argument name="modifiers" xsi:type="array">
        <item name="actual_qty" xsi:type="array">
            <item name="class" xsi:type="string">Vendor\Module\Ui\DataProvider\Product\Form\Modifier\ChangeValue</item>
            <item name="sortOrder" xsi:type="number">100</item>
        </item>
      </argument>
   </arguments>
 </virtualType>
</config>

Here is the code of data provider

<?php
namespace Vendor\Module\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Ui\DataProvider\Modifier\PoolInterface;
use Magento\Framework\Stdlib\ArrayManager;

class ChangeValue extends AbstractModifier
{
/**
 * @var PoolInterface
 */
private $pool;

/**
 * @var ArrayManager
 */
private $arrayManager;
protected $_coreRegistry;
protected $_resource;
private $stockRegistry;

public function __construct(
    ArrayManager $arrayManager,
    \Magento\Framework\Registry  $coreRegistry,
    \Magento\Framework\App\ResourceConnection $resource,
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
) {
    $this->arrayManager = $arrayManager;
    $this->_coreRegistry = $coreRegistry;
    $this->_resource = $resource;
    $this->stockRegistry = $stockRegistry;
}


public function modifyMeta(array $meta)
{
    $meta = $this->enableTime($meta);

    return $meta;
}

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

/**
 * Customise Custom Attribute field
 *
 * @param array $meta
 *
 * @return array
 */
protected function enableTime(array $meta)
{
    $fieldCode = 'actual_qty';

    $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;
}

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'])){
        $productStock = $this->stockRegistry->getStockItem($productId);
        $productQty = $productStock->getQty();
        $actualQty = $resultSets[0]['qty'];
        $availableQty = ($productQty - $actualQty);
        return $availableQty;
    }else{
        $productStock = $this->stockRegistry->getStockItem($productId);
        $availableQty = $productStock->getQty();
        return $availableQty;
    }

}
public function getConnection()
{
    $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    return $connection;
  }
}

This is not working for me. Can anyone look into it where i am doing wrong.

I need to set the value returned here to the custom attribute when the product is viewed from admin side.

"actual_qty" is the attribute code.

$sql query is to check if the product is added to cart.

Thanks

  • What is the exact requirement you want? – Sukumar Gorai Nov 28 '18 at 13:41
  • @SukumarGorai, When the product is viewed in admin panel, I am showing one text field with Actual Qty, That should fill with the result returned by the query i written. in this function getProductData() – jafar pinjar Nov 28 '18 at 13:46
  • @SukumarGorai, have you got the issue? – jafar pinjar Nov 28 '18 at 13:58
  • Did you created that attribute in admin? – Sukumar Gorai Nov 28 '18 at 13:59
  • @SukumarGorai, found anything about this issue? – jafar pinjar Nov 29 '18 at 8:02

Your Answer

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

Browse other questions tagged or ask your own question.