2

How to get the existing product id from my Model.

<?php

class SyncObserver implements ObserverInterface {

    function __construct
    (
    \Vendor\Module\Model\Engine $syncEngineFactory
    ) {
        $this->syncEngineFactory = $syncEngineFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $product = $observer->getEvent()->getProduct();
        $this->productUpdate($product);
        return $this;

   public function productUpdate($product){
     $productId = 1;
     $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
     $currentproduct = $objectManager->create('Vendor\Module\ResourceModel\Queue')->load($productId,'entity_id');   //Erro here   'Recoverable Error: Argument 1 passed to Magento\Framework\Model\ResourceModel\Db\AbstractDb::load()
    }
}

UPDATE:

Event is : catalog_product_save_after

<event name="catalog_product_save_after">
    <observer name="catalog_product_save_after_handler" instance="Vendor\Module\Observer\SyncObserver"/>
</event>

1 Answer 1

2

you can load product collection by below code:

 public function execute(\Magento\Framework\Event\Observer $observer)
 {
    $_product = $observer->getProduct()->getId();  // you will get product object
    $_sku=$_product->getSku(); // for sku

 }   

Complete Answer:

Create events.xml file in \module\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_after">
        <observer name="catalog_product_save_after_handler" instance="Vendor\Module\Observer\SyncObserver"/>
    </event>
</config>

Create observer file in Module\Observer\SyncObserver.php

<?php

class SyncObserver implements ObserverInterface {

    function __construct
    (
    \Vendor\Module\Model\Engine $syncEngineFactory
    ) {
        $this->syncEngineFactory = $syncEngineFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $product = $observer->getProduct()->getId();
        $this->productUpdate($product);
        return $this;

   public function productUpdate($product){
     //$productId = 1;
     $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
     $currentproduct = $objectManager->create('Vendor\Module\ResourceModel\Queue')->load($product,'entity_id');   //Erro here   'Recoverable Error: Argument 1 passed to Magento\Framework\Model\ResourceModel\Db\AbstractDb::load()
    }
}

Reference : Get product from save event observer - Magento 2

8
  • tried that..but i want to get the existing product id...
    – Sushivam
    Commented Aug 22, 2016 at 6:46
  • for which event you have created this observer? post event name anyone can guide on that direction. Commented Aug 22, 2016 at 6:47
  • its catalog_product_save_after event, i want to get the id during the product save
    – Sushivam
    Commented Aug 22, 2016 at 6:51
  • have you tried this $observer->getEvent()->getProduct()->getId(); Commented Aug 22, 2016 at 6:58
  • Check the updated execute() @Sarfaraz...im unable to get the product id using the load() as above , i get the erro
    – Sushivam
    Commented Aug 22, 2016 at 7:03

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.