I'm looking for how to get product id after product saved using plugin.
I use the plugin to catch after execute() is fired.
But I can't get product id no matter how I try.
Here is what I've done:

di.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Controller\Adminhtml\Product\Save">
        <plugin name="synergy_butility_save_product"
                type="Synergy\Butility\Plugin\Product\Save"
        />
    </type>
</config>

Save.php

<?php

namespace Synergy\Butility\Plugin\Product;

class Save
{
    public function __construct(
        \Magento\Framework\App\RequestInterface $request
    )
    {
        $this->_request = $request;
    }

    public function afterExecute(\Magento\Catalog\Controller\Adminhtml\Product\Save $subject, $result)
    {
        // AB-18 -fudu- update news_to_day take data from news_to_date in catalog_product_flat_32 table after "saved product" in admin page
        $productId = $subject->productId;

        $post = $this->_request->getPost();
        var_dump($post); exit;
        // $news_to_date = $subject->getProduct()->getNewsToDate();

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $resource = $objectManager->get('\Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $tableName = $resource->getTableName('catalog_product_flat_32');

        $query = "SELECT * FROM " . $tableName . " WHERE entity_id = " . $productId;

        $product = $connection->query($query);

        foreach ($product as $rows) {
            var_dump($rows);
        }
        var_dump('doesnt work');
        exit;
        $update_to_db = "UPDATE " . $tableName . " SET news_to_day = '" . $news_to_date . "'  WHERE entity_id = " . $productId;
        $connection->query($update_to_db);
    }
}

Thanks for reading :)

  • Please check my answer and let me know if your question not solve. – Rohan Hapani Oct 19 at 9:22
  • Is it working? @Fudu – Rohan Hapani Oct 19 at 9:44
  • please check my answer :) thanks for replying – fudu Oct 19 at 10:11
  • In plugin , did you check $this->_request->getParams(); and print that? You can also find product id from there. – Rohan Hapani Oct 19 at 10:12
  • Check my updated answer. – Rohan Hapani Oct 19 at 10:29

Instead of using a plugin, have you tried doing this with event e.g. catalog_product_save_after?

It works on both 'update' and 'create' e.g.

1) In file \app\code\Testspace\Test\etc\adminhtml\events.xml define:

<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="price" instance="Testspace\Test\Model\Observer\Save" />
    </event>
</config>

Then in your observer

namespace Testspace\Test\Model\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;


class Save implements ObserverInterface
{
    public function execute(EventObserver $observer)
    {
        echo '<pre>'; var_dump($observer->getProduct()->getId()); echo '</pre>';
        die();

        return true;
    }
}

Hope this helps.

  • i tried use observer but it only worked for "update product", not with "add product" @@ – fudu Oct 18 at 6:15

this is my solution based on this link
https://webkul.com/blog/mass-update-products-attribute-magento-2/
So basically if you follow this solution, you don't need to use query in observer anymore

Here is how i did:

ProductSaveObserver.php

<?php

namespace Synergy\Butility\Observer;

use Magento\Framework\Event\ObserverInterface;

class ProductSaveObserver implements ObserverInterface
{    
    public function __construct(
        \Magento\Framework\App\ResourceConnection $resource
    ) {
        $this->_resource = $resource;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // AB-18 update news_to_day take data from news_to_date in catalog_product_flat_32 table after "saved product" in admin page
        $productId = $observer->getProduct()->getId();

        $productIdArr = [$productId];

        $news_to_date = $observer->getProduct()->getNewsToDate();
        $news_to_date = substr($news_to_date,0,-8);
        $news_to_date = $news_to_date.'00:00:00';

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $productActionObject = $objectManager->create('Magento\Catalog\Model\Product\Action');
        $productActionObject->updateAttributes($productIdArr, array('news_to_day' => $news_to_date), 0);
    }
}

If you want to product object after saving product from backend then you can easily use catalog_product_save_after event.

Put this events.xml in below path app\code\vendor_name\module_name\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="vendor_name\module_name\Observer\Productsaveafter" />
    </event>
</config>

And put your Productsaveafter.php in below path app\code\vendor_name\module_name\Observer\

<?php

namespace vendor_name\module_name\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $_product = $observer->getProduct();  // you will get product object
        $product_id = $_product->getId(); // for product id
        $product_sku = $_product->getSku(); // for sku
    }   
}

EDIT :

If you want to get in plugin. Then, you can get by below line.

$product_id = $this->_request->getParam('id');
  • sorry but i want to use plugin, not observer. :( and i already have the answer, but for some reason, it deleted by admin of magento.stackexchange :( – fudu Oct 19 at 10:08

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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