1

How to save advance inventory custom field value in DB on Magento 2

enter image description here

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="cataloginventory_stock_item_save_after">
    <observer name="Vendor_Module::save_custom_data" 
         instance="Cm\Preorder\Observer\AfterProductSave"/>
  </event>
</config>

AfterProductSave.php

<?php
namespace Cm\Preorder\Observer;
use Magento\Framework\Event\ObserverInterface;
class AfterProductSave implements ObserverInterface
{
public function __construct(
    \Magento\Framework\App\RequestInterface $request
)
{
    $this->_request = $request;
}
 public function execute(\Magento\Framework\Event\Observer $observer)

 {
$post = $this->_request->getPost();
var_dump($post);
 }

}
  • in custom table ??? – Rk Rathod May 8 at 13:11
  • In upgrade schema of 'cataloginventory_stock_item' table – divya sekar May 8 at 13:12
  • I want to save a custom field in 'cataloginventory_stock_item' table prnt.sc/nm0ce5 – divya sekar May 8 at 13:15
1

created Observer which calls Cataloginventory Stock Item Save After event.

<?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="cataloginventory_stock_item_save_after">
        <observer name="Vendor_Module::save_custom_data" instance="Vendor\Module\Observer\AfterProductSave"/>
    </event>
</config>

And In Your AfterProductSave.php file You Can Add Your Custom Field Value

  • I will try and update you – divya sekar May 8 at 13:16
  • working or not ?? – Rk Rathod May 8 at 15:39
  • no, its not working – divya sekar May 9 at 5:06
  • any error or issue ??????? – Rk Rathod May 9 at 5:17
  • not yet but custom field values not saved in db – divya sekar May 9 at 5:19
1

In this case, you need to implement extension attribute for Magento\CatalogInventory\Api\Data\StockItemInterface.

Use these two fields as extension_attributes on Magento\CatalogInventory\Api\Data\StockItemInterface

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for=Magento\CatalogInventory\Api\Data\StockItemInterface">
        <attribute code="preorder_button" type="string" />
        <attribute code="preorder_note" type="string" />
    </extension_attributes>
</config>

Save on using Plugin

Then create plugin on Magento\CatalogInventory\Api\StockItemRepositoryInterface:save

and this attribute value.

Like below

<?php
namespace {YourNamespace};

class SaveDataPlugin {

    public function beforeSave
    (
        \Magento\CatalogInventory\Api\StockItemRepositoryInterface $subject,
        \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem
    ) {
        $extensionAttributes = $subject->getExtensionAttributes();
        if (
                null !== $extensionAttributes &&
                null !== $extensionAttributes->getPreorderButton()
        ) {
            $Value = $extensionAttributes->getPreorderButton()->getValue();
            $stockItem->setPreorderButton($Value);
        }
        if (null !== $extensionAttributes &&
            null !== $extensionAttributes->getPreorderNote()
        ) {
            $Value = $extensionAttributes->getPreorderNote()->getValue();
            $stockItem->setPreorderNote($Value);
        }        
        return $stockItem;
    }

}

Note that code is posted base on Idea, not Test

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.