1

I am a newbie for magento 2 and try to save data into custom table when user submit a product data in backend.

I tried to use di.xml and events.xml, but I still don't know when and where do this action insert the data into table.

Because there is basic on the exist page that is product submit page, not a new page to do it.

So, I am confused the data flow.

Updated:

I used the events.xml to do it. but not working for me. there is the file code:

Demo\User\etc\adminhtml\events.xml:

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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="demo_user_product" instance="Demo\User\Model\Product" method="testEvent" />
    </event>
</config>

Demo\User\Model\Product:

<?php
namespace Demo\User\Model;
use Magento\Framework\Model\AbstractModel as AbstractModel;
class Product extends AbstractModel
{
    public function testEvent()
    {
        $myfile = fopen("/file_path/Product.txt", "w");
        fwrite($myfile, 1);
        fclose($myfile);
    }
}

when I click the save button of product in backend, there is no data is written into "Product.txt" file. this file's permission is 777. so, anybody know what's wrong in my code?

  • anybody know how to solve my question? need your help. – Mike Zhang Mar 10 '16 at 2:03
  • I used the events.xml to do it. but not working for me. – Mike Zhang Mar 10 '16 at 7:55
  • which Magento version? method attribute is not valid (check magento/framework/Event/etc/events.xsd:48) An observer must implements \Magento\Framework\Event\ObserverInterface, so you need to define execute method – bchatard Mar 10 '16 at 8:06
  • @Brice version is 2.0.2. i changed the function name to execute, but it don'ts still work. – Mike Zhang Mar 10 '16 at 8:30
  • there is also a typo in your XML <event name="controller_action_catalog_product_save_entity_after"> (a quote) – bchatard Mar 10 '16 at 8:36
3

You can use events for this. But I recommend using the event catalog_product_save_after instead of controller_action_catalog_product_save_entity_after.

Here is what you need. Let's say your module name is Demo_User.

app/code/Demo/User/registration.php - the registration file

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Demo_User',
    __DIR__
);

app/code/Demo/User/etc/module.xml - the module declaration file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Demo_User" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

app/code/Demo/User/etc/adminhtml/events.xml - the event declaration file

<?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="some_unique_name_here" instance="Demo\User\Observer\Adminhtml\Product" />
    </event>
</config>

app/code/Demo/User/Observer/Adminhtml/Product.php - the actual observer.

<?php 
namespace Demo\User\Observer\Adminhtml;

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

class Product implements ObserverInterface
{
    public function __execute(Observer $observer)
    {
         //your code here
         //you can access the product being saved like this
         $product = $observer->getEvent()->getProduct();
    }
}

run in the console php bin/magento setup:upgrade and you should be good to go.

| improve this answer | |
  • Thanks for your help. I have modified my code according @Brice to solve my question. I will check your advice whether works for me or not – Mike Zhang Mar 10 '16 at 13:38
  • I your code is working, too. but i can get the product data with "$observer->getEvent()->getProduct();". once i used this code, i will get a blank page after refresh the product editing page. so, what's wrong? – Mike Zhang Mar 11 '16 at 5:44
  • Add ->getData after to get only the product data – Marius Mar 11 '16 at 6:05
  • I know this is Old thread. But will this Event work for Product Bulk Import as well ?? – JItendra Rana Jun 8 '17 at 7:54
  • @JItendraRana. No it doesn't. It works only for when you specifically call the save method from the product model. – Marius Jun 8 '17 at 7:59

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.