0

I need to change product attribute value after saving it, but it gives me requested product doesn't exist when I save the product

 class myClass extends Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
 { 
public function afterSave($object){
    // your after save logic


    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productRepository = $objectManager->create('\Magento\Catalog\Api\ProductRepositoryInterface');
    $product = $productRepository->getById($productId);
    $product->setName($productName);
    $productRepository->save($product);

    return parent::afterSave($object);
}
  • $productId is undefined – Marius Jan 19 at 19:25
  • No, it's defined, I've defined it – Yomna Mansour Jan 19 at 20:23
0

You should use "beforeSave" when saving data.

In your case it maybe loop forever

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.