0

I'm trying to update 2 custom field I have created by the db_schema.xml file into my catalog_product_entity thats part working as its shut be and everything are working smooth.

Now I'm trying to call a event on catalog_product_save_after and trying to update a my to new fields.

The problem is I can't find a way around for it, I hope some one can help me here.

My code is this, I'm trying with my very simple code here and need the last part of it, but can't find a way for it.

<?php

namespace SmartPack\WMS\Event;

use Magento\Framework\Event\ObserverInterface;

class ProductChanged implements ObserverInterface
{
    public function __construct()
    {
        // Observer initialization code...
        // You can use dependency injection to get any class this observer may need.
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getProduct();
        $sku = $product->getSku();
        $name = $product->getName();
        $id = $product->getId();

        echo 'productId: ' . $id;

        exit;
    }
}

There can be different ways for me to update, and what I hope I can get help to its open a custom SQL connection so I just can execute my own SQL code so I update the fields I need when my Observer its hitten.

2 Answers 2

0

According to magento standard,

  1. Event Observer do not use to modify any data.

  2. If you want to modify the entity data, then you need to use the Plugin.

3
  • Based on a Event Observer I need to update 2 fields inside the table. What I want its listen when a user save data on a product, and when its happen, I need to update 2 fields inside the database I need for later use. If its a Plugin I need can you help me to the right way or? Aug 17, 2021 at 7:38
  • Yes, ask the separate question for that one and other community member cal also help you. And in current question if my answer help you then upvote and accept as solution so it will help other in community. Aug 17, 2021 at 9:10
  • I think you misunderstand me, I want to change a field in my product SQL its not change product data, I have read up for Observer and Plugin now, and I did not need to use a Plugin, I to get my Observer to work, so I can open a SQL connection where I just can update with custom SQL. Aug 17, 2021 at 19:26
0

I found a way around what I seek, just note if you use a "exit" to block the code to debug, then the database query will not be executed success.

<?php

namespace SmartPack\WMS\Event;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\ResourceConnection;

class ProductChanged implements ObserverInterface
{
    private $resourceConnection;

    public function __construct(ResourceConnection $resourceConnection)
    {
        $this->resourceConnection = $resourceConnection;

    }

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

        $connection  = $this->resourceConnection->getConnection();
        $data = [
            "field_1" => 'value_1'
        ];

        $where = ['entity_id = ?' => (int)$id];

        $tableName = $connection->getTableName('catalog_product_entity');
        $updatedRows = $connection->update($tableName, $data, $where);
    }
}

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.