1

I want to update the "customer_entity" table on adminhtml_customer_save_after event and trying to do so with the help of an observer. How can i do that?

4
  • what changes you want?
    – Amit Bera
    Nov 8, 2017 at 12:58
  • I am using customer activation, if the customer is activated i want the 'isactive' field to be updated.' Nov 8, 2017 at 13:04
  • how we we know that customer is active ?
    – Amit Bera
    Nov 8, 2017 at 13:15
  • Thats what I am trying to do, I have placed a check box on admin html customer information tab. If the user checks that and save the customer then "customer_entity table" should get updated. Nov 8, 2017 at 13:21

1 Answer 1

0

try this observer for save customer data:

<?php
namespace {VendorName}\{Vendorname}\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Api\CustomerMetadataInterface;
/**
 * @category   Devamitbera
 * @package    Devamitbera_Common
 * @author     Amit Bera <[email protected]>
 * @website    http://www.amitbera.com
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
class {ClassName}  implements ObserverInterface
{

    protected $_customerRepository;
    public function __construct(
        \Magento\Customer\Api\CustomerRepositoryInterface = $_customerRepository;

    ) {
        $this->_customerRepository = $_customerRepository;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $customerId = $this->getCurrentCustomerId($observer->getRequest());
        $customer = $observer->getCustomer();

        if ($customer->getId() && 
            (boolean)$observer->getRequest()->getParam('{Your_Custom_Field}', false)) {
            $customer->setCustomAttribute('is_active', true);
            $this->_customerRepository->save($customer);
        }

     }

}
2
  • I am already using the "CustomerRepositoryInterface" tried to do so with that object also but its not working, 'is_active' is not a custom attribute of customer_entity table. Nov 8, 2017 at 14:41
  • Any other way how can i directly make database changes from observer? Nov 8, 2017 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.