0

I want to create an event observer when admin update attribute from admin panel. i've tried to use eav_entity_attribute_save_after and eav_entity_attribute_delete_after, but it's not working I have not found any suitable event observer. is in magento2 events for update eav attributes?

2

If you want to $productobj after saving product from backend side so you can easily use catalog_product_save_after event.

I am assuming that you already know how to create a module in M2.

Right now you have to need develop new module for M2

Then Create this events.xml file in below path

app\code\YOUR_NAMESPACE\YOURMODULE\etc\adminhtml

<?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="YOUR_NAMESPACE\YOUR_MODULENAME\Observer\Productsaveafter" />
    </event>
</config>

And Create your observer file Productsaveafter.php in below path

app\code\YOUR_NAMESPACE\YOURMODULE\Observer\

<?php

namespace YOURNAMESPACE\YOURMODULENAME\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        $id = $product->getId(); //Get Product Id

        //Get Quantity
        $stockItem = $product->getExtensionAttributes()->getStockItem();
        $stockData = $stockItem->getQty();
        // Get new Qty
        $_vendor_qty = $product->getVendorQty();
        $_on_hand_qty = $product->getOnHandQty();
        $totalQty = $_vendor_qty+$_on_hand_qty; //Add New Qty


        $stockItem->setQty($totalQty); //Set New Qty to Main Qty
        $stockItem->save();

    }   
}
| improve this answer | |
1

You should try use afterSave plugin on Attribute ResourceModule instead

| improve this answer | |
  • Do you have example? – omelandr Feb 10 '17 at 15:34
  • You can see on \Magento\Framework\App\Cache\FlushCacheByTags plugin that cleanup cache after entity is saved – KAndy Feb 10 '17 at 21:38
0

Maybe this will help catalog_entity_attribute_save_before / catalog_entity_attribute_save_after

1) Create events.xml inside Vendor/Module/etc/ and add below code inside events.xml

For catalog_entity_attribute_save_before Add This :

<?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_entity_attribute_save_before">
        <observer name="catalog_entity_attribute_save_before_observer" instance="Vendor\Module\Observer\AttributeSaveBefore"/>
    </event>
</config>

For catalog_entity_attribute_save_after Add This :

<?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_entity_attribute_save_after">
        <observer name="catalog_entity_attribute_save_after_observer" instance="Vendor\Module\Observer\AttributeSaveAfter"/>
    </event>
</config>

2.1) For catalog_entity_attribute_save_after Create AttributeSaveAfter.php inside Vendor/Module/Observer/ and add below code inside AttributeSaveAfter.php

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Catalog\Model\Product\Attribute\Repository as AttributeRepository;

class AttributeSaveAfter implements ObserverInterface
{
    /**
     * @var AttributeRepository
     */
    private $attributeRepository;

    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $config,
        AttributeRepository $repository,
        array $data = []
    ) {
        $this->_scopeConfig = $config;
        $this->attributeRepository = $repository;
    }

    public function execute(EventObserver $observer)
    {

        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES;

        $attrCode = $observer->getEvent()->getAttribute()->getAttributeCode();

        if ($attrCode == 'your_attribute_code_for_compare') {

        }
    }
}

2.2) For catalog_entity_attribute_save_before Create AttributeSaveBefore.php inside Vendor/Module/Observer/ and add below code inside AttributeSaveBefore.php

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Catalog\Model\Product\Attribute\Repository as AttributeRepository;

class AttributeSaveBefore implements ObserverInterface
{
    /**
     * @var AttributeRepository
     */
    private $attributeRepository;

    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $config,
        AttributeRepository $repository,
        array $data = []
    ) {
        $this->_scopeConfig = $config;
        $this->attributeRepository = $repository;
    }

    public function execute(EventObserver $observer)
    {

        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES;

        $attrCode = $observer->getEvent()->getAttribute()->getAttributeCode();

        if ($attrCode == 'your_attribute_code_for_compare') {

        }
    }
}
| improve this answer | |

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.