We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

We will achieve this functionality through EventObserver.

Here, I have used the event which is called

“controller_action_catalog_product_save_entity_after”

Step 1: Create event.xml in /etc/adminhtml folder in your module.

<?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="controller_action_catalog_product_save_entity_after">
        <observer name="magemonkeys_saveproductlogic" instance="MagemonkeysSaveproductlogicObserverSaveProduct" />
    </event>
</config>

Step 2: Create a SaveProduct.php Observer in Observer Folder.

<?php
namespace MagemonkeysSaveproductlogicObserver;

use MagentoFrameworkEventObserverInterface;

class SaveProduct implements ObserverInterface
{
    protected $catalogData;

    protected $_resource;

    
    public function __construct(
        MagentoFrameworkAppResourceConnection $resource
        )
    {
        $this->_resource = $resource;
    }

    
    public function execute(MagentoFrameworkEventObserver $observer)
    {
        $connection = $this->_resource->getConnection();
        $table_name = $this->_resource->getTableName('product_oem');
        $productController = $observer->getController();
        $data = $productController->getRequest()->getPostValue();

        $mainProduct = $observer->getProduct();
        $productId = $mainProduct->getId();
        /* You can use you logic here */

        if(isset($data['product']['product_oem']) && $productId){
            $connection->query('DELETE FROM ' . $table_name . ' WHERE product_id =  ' . (int)$productId . ' ');
            $productOems = $data['product']['product_oem'];
            if(!is_array($productOems)){
                $productOems = array();
                $productOems[] = (int)$data['product']['product_oem'];
            }
            foreach ($productOems as $k => $v) {
                $connection->query('INSERT INTO ' . $table_name . ' VALUES ( ' . $v . ', ' . (int)$productId . ',0)');
            }
        }
    }
}
?>

After creating the above module, please run the upgrade, compile and static:content:deploy command to implement this module.

field_5bfb909c5ccae

    Recent Articles
    Get a Free Quote

      Let’s initiate a discussion!!