1

app/code/Cm/Preoder/view/adminhtml/ui_component/product_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <modal name="advanced_inventory_modal">
        <fieldset name="stock_data">
            <container name="container_backorders">
                <field name="backorders">
                    <argument name="data" xsi:type="array">
                        <item name="config" xsi:type="array">
                            <item name="component" xsi:type="string">Cm_Preorder/js/backorders</item>
                        </item>
                    </argument>
                </field>
             </container>
                <field name="preorder_button">
                    <argument name="data" xsi:type="array">
                        <item name="config" xsi:type="array">
                            <item name="dataType" xsi:type="string">text</item>
                            <item name="label" xsi:type="string" translate="true"> Pre-Order Cart Button</item>
                            <item name="scopeLabel" xsi:type="string">[Store view]</item>
                            <item name="formElement" xsi:type="string">input</item>
                            <item name="source" xsi:type="string">model</item>
                            <item name="sortOrder" xsi:type="number">801</item>
                            <item name="dataScope" xsi:type="string">preorder_button</item>
                        </item>
                    </argument>
                </field>
                <field name="preorder_note">
                    <argument name="data" xsi:type="array">
                        <item name="config" xsi:type="array">
                            <item name="dataType" xsi:type="string">text</item>
                            <item name="label" xsi:type="string" translate="true">  Pre-Order Note</item>
                            <item name="scopeLabel" xsi:type="string">[Store view]</item>
                            <item name="formElement" xsi:type="string">input</item>
                            <item name="source" xsi:type="string">model</item>
                            <item name="sortOrder" xsi:type="number">802</item>
                            <item name="dataScope" xsi:type="string">preorder_note</item>
                        </item>
                    </argument>
                </field>
            </fieldset>
    </modal>
</form>

app/code/Cm/Preorder/etc/adminhtml/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="preorder" instance="Cm\Preorder\Observer\ProductSaveAfter" />
    </event>
</config>

app/code/Cm/Preoder/Observer/ProductSaveAfter.php

<?php
namespace Cm\Preorder\Observer;

use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer as EventObserver;

class ProductSaveAfter implements ObserverInterface
{
     protected $_scopeConfig;
     protected $_request;
     protected $_objectManager;


     public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\ObjectManagerInterface $objectmanager


    ) {
        $this->_objectManager = $objectmanager;
        $this->_scopeConfig = $scopeConfig;
        $this->_request = $request;

    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

         $_product = $observer->getProduct();  // you will get product object 
        $product_id = $_product->getId();
        $preorder_button=$_product->getPreorderButton(); 
        $preorder_note=$_product->getPreorderNote();
        $resource = $this->_objectManager->get('Magento\Framework\App\ResourceConnection');
        $query = "SELECT * FROM `cataloginventory_stock_item` WHERE  `product_id`=$product_id";
        $connection = $resource->getConnection();
         $res = $connection->fetchAll($query);
        if($res)
            {
               $var = array_column($res, 'product_id');
                $key = key($var);
                $id = $var[$key];
                $sql = "UPDATE `cataloginventory_stock_item` SET preorder_button = '$preorder_button' , preorder_note ='$preorder_note'  WHERE product_id = ".$id;
                $connection->query($sql);
            }
          }
}

In product edit form, custom field shows empty after value saved in db

enter image description here

Saved value

enter image description here

3

Try using plugin how you save:

app/code/SR/MagentoCommunity/etc/adminhtml/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\CatalogInventory\Model\Stock\StockItemRepository">
        <plugin name="sr_update_stock_item"
                type="SR\MagentoCommunity\Plugin\CatalogInventory\Model\Stock\StockItemRepository" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoCommunity/Plugin/CatalogInventory/Model/Stock/StockItemRepository.php

<?php
namespace SR\MagentoCommunity\Plugin\CatalogInventory\Model\Stock;

use Magento\CatalogInventory\Model\ResourceModel\Stock\Item as StockItemResource;
use Magento\Framework\Registry;

class StockItemRepository
{
    /**
     * @var StockItemResource
     */
    private $resource;

    /**
     * @var Registry
     */
    private $registry;

    /**
     * StockItemRepository constructor.
     *
     * @param StockItemResource $resource
     * @param Registry $registry
     */
    public function __construct(
        StockItemResource $resource,
        Registry $registry
    ) {
        $this->resource = $resource;
        $this->registry = $registry;
    }

    public function afterSave(
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $subject,
        $stockItem
    ) {
        $product = $this->registry->registry('product');
        $preorderButton = $product->getData('preorder_button');
        $preorderNote = $product->getData('preorder_note');
        $needSave = false;
        $newStockItem = $stockItem;
        if ($preorderButton) {
            $newStockItem->setPreorderButton($preorderButton);
            $needSave = true;
        }
        if ($preorderNote) {
            $newStockItem->setPreorderNote($preorderNote);
            $needSave = true;
        }

        if ($needSave) {
            $this->resource->save($newStockItem);
        }

        return $stockItem;
    }
}

How populate when edit:

app/code/SR/MagentoCommunity/etc/adminhtml/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\CatalogInventory\Ui\DataProvider\Product\Form\Modifier\AdvancedInventory">
        <plugin name="sr_load_stock_item"
                type="SR\MagentoCommunity\Plugin\CatalogInventory\Ui\DataProvider\Product\Form\Modifier\AdvancedInventory" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoCommunity/Plugin/CatalogInventory/Ui/DataProvider/Product/Form/Modifier/AdvancedInventory.php

<?php
namespace SR\MagentoCommunity\Plugin\CatalogInventory\Ui\DataProvider\Product\Form\Modifier;

use Magento\CatalogInventory\Model\Stock\StockItemRepository;

class AdvancedInventory
{
    /**
     * @var StockItemRepository
     */
    private $stockItemRepository;

    /**
     * AdvancedInventory constructor.
     *
     * @param StockItemRepository $stockItemRepository
     */
    public function __construct(
        StockItemRepository $stockItemRepository
    ) {
        $this->stockItemRepository = $stockItemRepository;
    }

    public function afterModifyData(
        \Magento\CatalogInventory\Ui\DataProvider\Product\Form\Modifier\AdvancedInventory $subject,
        $data
    ) {
        foreach ($data as &$product) {
            if (isset($product['product']['stock_data']) && isset($product['product']['stock_data']['item_id'])) {
                $itemId = $product['product']['stock_data']['item_id'];
                if ($itemId) {
                    try {
                        $item = $this->stockItemRepository->get($itemId);
                        $product['product']['preorder_button'] = $item->getData('preorder_button');
                        $product['product']['preorder_note'] = $item->getData('preorder_note');;
                    } catch (\Exception $e) {}
                }
            }
        }

        return $data;
    }
}
  • It works what i am expect thank u so much @Sohel Rana – divya sekar May 10 at 5:12
  • can u pls help me to solve this issue – divya sekar May 31 at 5:20
  • when checkout its loading and produce console error <br /> <b>Fatal error</b>: Call to a member function getData() on null in <b>/home/bootsgri/public_html/magento2/app/code/cm/Preorder/Plugin/CatalogInventory/Model/Stock/StockItemRepository.php</b> on line <b>38</b><br /> {"messages":{"error":[{"code":500,"message":"Server internal error. See details in report api\/1308459485642"}]}} – divya sekar Jun 3 at 7:26
  • Please help me @Sohel – divya sekar Jun 3 at 7:27
  • in localhost its working well but in server not wrking – divya sekar Jun 3 at 7:29

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.