Skip to content

Update product stock data Progratically

i have a scenario where i am trying to update product data programatically and setting manage stock to no this works great for the product created in the admin.

but i have situation here if the product are inserted through scripted directly in to DB this catalog_product_save_before event is not called so my code fails in this condition.

anyone knows or have any idea how i can fix this below is the way i was doing it.

<event name="catalog_product_save_before">
        <observer name="test_name" instance="MageplazaHelloWorldObserverProductsaveafter" />
    </event>

Observer

<?php

namespace MageplazaHelloWorldObserver;

use MagentoFrameworkEventObserverInterface;

class Productsaveafter implements ObserverInterface
{
    public function execute(MagentoFrameworkEventObserver $observer)
    {
        $productCreated = $observer->getProduct();
        $productCreated->setStockData(
            array(
                'use_config_manage_stock' => 0,
                'manage_stock' => 0
            )
        );
    }
}

let me know if someone can help me.