4

I have a quick question, how can we get the POST data inside the execute method of Productsaveafter class? I added new input fields in the Product form page in admin and would like to save them onto a new table.

class Productsaveafter implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getEvent()->getRequest(); 
        $productData = $product->getData('params');
        echo "<pre>"; print_r($product); exit;
    }   
}
  • You could try: $post = $this->getRequest()->getPostValue(); – tecjam Apr 13 '16 at 14:17
  • unfortunately, it does not work. i also tested $observer->getRequest()->getPostValue(); also error. although when I do a print_r($observer) I can see the post data. – Rahahaha Apr 13 '16 at 14:26
  • this is the one I see, i just don;'t have any idea how to extract it: [postParams:protected] => Zend\Stdlib\Parameters Object ( [storage:ArrayObject:private] => Array ( [form_key] => tiQ1H13YxgAHBLPi [product] => Array ( [name] => testAfterSave [sku] => testAfterSave [price] => 1,200.00 – Rahahaha Apr 13 '16 at 14:36
  • maybe you could use the toArray() function and then get it from the array? – tecjam Apr 13 '16 at 14:52
14

In your observer constructor do:

public function __construct(
    \Magento\Framework\App\RequestInterface $request
)
{
    $this->_request = $request;
}

Then in your code use $this->_request to fetch post params.

  • i tried this and this is indeed what i am looking for. Thanks Petar. – Rahahaha Apr 14 '16 at 5:39
0

You can get data from $oberver, example bellow :

$this->_eventManager->dispatch(
   'controller_action_catalog_product_save_entity_after',
   ['controller' => $this, 'product' => $product]
);

In this example product is pass to event, so in your observer, you can get this variable :

 public function execute(Observer $observer)
{
    $product = $observer->getData('product');
    $sku = $product->getSku();
}

Hope this help someone :) Will

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.