0

I would like to set Notify Qty from a script that reads a CSV file with Source, SKU and NotifyQty. It would probably need to enable notify qty for the products as well.

I can do it with SQL query to inventory_low_stock_notification_configuration.notify_stock_qty, but that's probably not the right way.

6
  • Hi! You have sku and qty inside some csv file and you want to import it from PHP script, is that right?
    – dudzio
    May 29 '21 at 10:13
  • Hi! Yes, that's right. I have the rest of the script ready, so it's reading the csv, but I don't know how to access the notify qty correctly
    – Dandy
    May 29 '21 at 10:16
  • Ok, let me prepare answer for you :)
    – dudzio
    May 29 '21 at 10:21
  • Could you change your answer so it contains information about import from CSV file?
    – dudzio
    May 29 '21 at 10:22
  • Are you ok with cli run script?
    – dudzio
    May 29 '21 at 10:24
0

This is example CSV file (sku_qty.csv):

source_code,sku,qty
default,SOME_SKU1,2
default,SOME_SKU2,3
default,SOME_SKU3,4

This is the code to run:

<?php

require __DIR__ . './../app/bootstrap.php';

use Magento\Framework\App\Bootstrap;

// Init Magento
$bootstrap = Bootstrap::create(BP, $_SERVER);

// Get Object Manager instance
$objectManager = $bootstrap->getObjectManager();

// Construct Factory object
/* @var $sourceItemConfigurationFactory \Magento\InventoryLowQuantityNotification\Model\SourceItemConfigurationFactory */
$sourceItemConfigurationFactory = $objectManager->get('Magento\InventoryLowQuantityNotification\Model\SourceItemConfigurationFactory');

// Construct Multiple Save object
/* @var $saveMultiple \Magento\InventoryLowQuantityNotification\Model\ResourceModel\SourceItemConfiguration\SaveMultiple */
$saveMultiple = $objectManager->get('Magento\InventoryLowQuantityNotification\Model\ResourceModel\SourceItemConfiguration\SaveMultiple');


// Create CSV file handle
$inputFile = fopen(__DIR__ . "/sku_qty.csv", "r");

// Get CSV headers
$headers = fgetcsv($inputFile);

// Init array of config objects
$sourceItemsConfiguration = [];

// Iterate CSV file
$n = 0;
while (($row = fgetcsv($inputFile)) !== false) {
    $n++;
    // Init source item config instance and set data
    $sourceItemConfiguration = $sourceItemConfigurationFactory->create();
    $sourceItemConfiguration->setSourceCode($row[0]);
    $sourceItemConfiguration->setSku($row[1]);
    $sourceItemConfiguration->setNotifyStockQty($row[2]);

    $sourceItemsConfiguration[] = $sourceItemConfiguration;
}

try {
    // Save data
    $saveMultiple->execute($sourceItemsConfiguration);
} catch (\Exception $e) {
    echo $e->getMessage();
}

I like to put my file inside script directory, so I run them like that php script/test.php from Magento root directory.

3
  • Thank you so much @dudzio, it worked like a charm! I've upvoted, but I have not reached 15 reps yet.
    – Dandy
    May 29 '21 at 12:05
  • Happy to hear! :) Thanks! I tried to run save() method on $sourceItemConfiguration inside while loop, but unfortunately there is a bug that results in The resource isn't set. error :(
    – dudzio
    May 29 '21 at 12:09
  • 1
    That's what I was trying too, but I thought it was my lack of knowledge, not a bug! Happy that you found a different solution!
    – Dandy
    May 29 '21 at 12:16

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.