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.
1 2 3 4 5 6 |
<?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="Magemonkeys\Saveproductlogic\Observer\SaveProduct" /> </event> </config> |
Step 2: Create a SaveProduct.php Observer in Observer Folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php namespace Magemonkeys\Saveproductlogic\Observer; use Magento\Framework\Event\ObserverInterface; class SaveProduct implements ObserverInterface { protected $catalogData; protected $_resource; public function __construct( \Magento\Framework\App\ResourceConnection $resource ) { $this->_resource = $resource; } public function execute(\Magento\Framework\Event\Observer $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.
Update product attribute value programmatically in Magento 2 . [crayon-62877538caf0a927110396/]...
If you want restrict customer to checkout based on your...
Sometime we need to set html data without load or...
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-62877538cb53c028252025/] Create...