1

i have created an observer, event on before product save to set custom product attribute value, here is my code.

etc/adminhtml/events.xml

<?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_before">
        <observer name="product_save_before" instance="Learning\HelloPage\Observer\Productsavebefore" />
    </event>
</config>

Learning/HelloPage/Observer/Productsavebefore.php

<?php

namespace Learning\HelloPage\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsavebefore implements ObserverInterface
{    
    protected $authSession;

    public function execute(\Magento\Framework\Event\Observer $observer,
        \Magento\Backend\Model\Auth\Session $authSession)
    {
        $this->authSession = $authSession;
        $admin_user_id = $this->authSession->getUser()->getId();

        $_product = $observer->getProduct();  // get product object
        //set the the attribute vale
        $_product->setAttribute(admin_user_id, $admin_user_id) ?? what to do here?
    }       
}
0

Try this

<?php

namespace Learning\HelloPage\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsavebefore implements ObserverInterface
{    
    protected $authSession;

    public function execute(\Magento\Framework\Event\Observer $observer,
        \Magento\Backend\Model\Auth\Session $authSession)
    {
        $this->authSession = $authSession;
        $admin_user_id = $this->authSession->getUser()->getId();

        $_product = $observer->getProduct();  // get product object
        //set the the attribute vale
        $_product->setAdminUserId($admin_user_id);
    }       
}

But you can save the product attribute while editing the product in backend itself.

  • my frontname label for the attribute is 'admin_user_id' so what would it be? like $_product->setadmin_user_id($id) – Jay Kapoor Sep 6 '17 at 6:14
  • No. It will be $_product->setAdminUserId('yourvalue'); – Agnes Sep 6 '17 at 6:19
  • its not working, and keeps loading the page and give the memory allocation error – Jay Kapoor Sep 6 '17 at 6:38
  • check now. I have edited my answer. I removed $_product->save(). Since it is a product save observer, $_product->save() makes a recursive call which leads to memory allocation error. – Agnes Sep 6 '17 at 7:07
  • got this error SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction, query was: INSERT INTO catalog_product_entity_int (entity_id,attribute_id,value,store_id) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE entity_id = VALUES(entity_id), attribute_id = VALUES(attribute_id), value = VALUES(value), store_id = VALUES(store_id) – Jay Kapoor Sep 6 '17 at 7:25

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.