I added additional fields to the Product New/Edit form and need to save them onto a new table in the database as well as post the data onto another website store api. I have created the function to perform all of this but, I am not sure how this should be called. Should I hardcode the function call inside the Product Save execute() function? or should I use the Productsaveafter observer? How can I get the post data inside the execute function of Productsaveafter observer?



This solution worked for me:

We need to use RequestInterface to get post.

$post_data = $this->_request->getPost()->toArray();


<?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="test_name" instance="Rahaha\RahahaWebService\Observer\Productsaveafter" />


namespace Rahaha\RahahaWebService\Observer;

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

class Productsaveafter implements ObserverInterface
    protected $_request;
    protected $_resource;
    protected $_scopeConfig;
    protected $_rahaha;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\App\ResourceConnection $resource,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
        $this->_request = $request;
        $this->_resource = $resource;
        $this->_scopeConfig = $scopeConfig;

        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $rahaha_username = $this->_scopeConfig->getValue("adminsample/rahaha_module_credentials_grp/rahaha_username", $storeScope);
        $rahaha_password = $this->_scopeConfig->getValue("adminsample/rahaha_module_credentials_grp/rahaha_password", $storeScope);
        $rahaha_api_key = $this->_scopeConfig->getValue("adminsample/rahaha_module_credentials_grp/rahaha_api_key", $storeScope);
        $rahaha_module_status = $this->_scopeConfig->getValue("adminsample/rahaha_module_status_grp/rahaha_module_status", $storeScope);

        $rs = $this->_resource;

        $this->_rahaha = new \Rahaha\RahahaWebService\Model\Rahaha\Rahaha($rahaha_username, $rahaha_password, $rahaha_api_key, $rahaha_module_status, $rs);

    public function execute(\Magento\Framework\Event\Observer $observer)
        $post_data = $this->_request->getPost()->toArray();

        //save stuff here...
| improve this answer | |
  • Can you please explain line "//save stuff here..." - how to save data here? – Fred Orosko Dias Apr 20 '17 at 14:00

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.