I have created some setting in Store->Configuration in Magento 2 i want to hit a controller if the setting is changed otherwise the i want the controller didn't hit.
How would i do that ?
My observer file path ( app/code/Custom/Code/Observer/ConfigObserver.php )
<?php
namespace Custom\Code\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Psr\Log\LoggerInterface as Logger;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\ResourceModel\Product\Action;
class ConfigObserver implements ObserverInterface
{
/**
* @var Logger
*/
protected $logger;
private $productAction;
protected $_productCollectionFactory;
protected $_pageFactory;
private $scopeConfig;
private $_objectManager;
/**
* @param Logger $logger
*/
public function __construct(
Logger $logger,
Action $productAction,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\Magento\Framework\ObjectManagerInterface $objectmanager,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
) {
$this->logger = $logger;
$this->productAction = $productAction;
$this->_pageFactory = $pageFactory;
$this->scopeConfig = $scopeConfig;
$this->_objectManager = $objectmanager;
$this->_productCollectionFactory = $productCollectionFactory;
}
public function execute(EventObserver $observer)
{
$attributes = [
'status' => 2,
'price' => 123
];
$productIds = array(10);
$storeId = 0;
$this->productAction->updateAttributes($productIds, $attributes, $storeId);
}
}
system.xml file path ( app/code/Custom/Code/etc/adminhtml/system.xml )
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="am_general" translate="label" sortOrder="10">
<label>Rule configuration</label>
</tab>
<section id="am_shipping" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top am-general-admin-tab-general</class>
<label>Rule Section</label>
<tab>am_general</tab>
<resource>Custom_Code::general_config</resource>
<group id="prices" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Rule settings</label>
<field id="add_item" translate="label" sortOrder="15" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Rule based on price</label>
<frontend_model>Custom\Code\Block\Adminhtml\Menu\Field\AdditionalItem</frontend_model>
<backend_model>Custom\Code\Block\Adminhtml\Menu\Config\Backend\AdditionalItem</backend_model>
</field>
</group>
</section>
</system>
And my event name is admin_system_config_changed_section_am_shipping.
Any help is appreciated.