working solution for magento 2.2.6
csv file look like
sku,attribute_name
ABC,[attribute_id]
script:
<?php $file = fopen('custom_status.csv', 'r', '"'); // set path to the CSV file
// put inside the magento root, run it by command line "php -f UpdateCustomStatus.php"
if ($file !== false) {
//use Magento\Framework\App\Bootstrap;
/**
* If your external file is in root folder
*/
require __DIR__ . '/app/bootstrap.php';
/**
* If your external file is NOT in root folder
* Let's suppose, your file is inside a folder named 'xyz'
*
* And, let's suppose, your root directory path is
* /var/www/html/magento2
*/
//$rootDirectoryPath = '/public_html';
//require $rootDirectoryPath . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
// used for updating product info
$productRepository = $objectManager->get('Magento\Catalog\Model\ProductRepository');
// used for updating product stock
$stockRegistry = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');
// add logging capability
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/import-update.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
// enter the number of data fields you require the product row inside the CSV file to contain
$required_data_fields = 2; //change for 3 to 2
$header = fgetcsv($file); // get data headers and skip 1st row
while ( $row = fgetcsv($file, 350, ",") ) { //change to 350 at a time
$data_count = count($row);
if ($data_count < 1) {
continue;
}
$data = array();
$data = array_combine($header, $row);
$sku = $data['sku'];
if ($data_count < $required_data_fields) {
$logger->info("Skipping product sku " . $sku . ". Not enough data to import.");
continue;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////updating price/////////////////////////////////////////////////////////////////////
try {
$product = $productRepository->get($sku);
}
catch (\Exception $e) {
$logger->info("Invalid product SKU: ".$sku);
continue;
}
// You can set other product data with $product->setAttributeName() if you want to update more data
//if ($product->getPrice() != $price) {
// $product->setPrice($price)
// ->setStoreId(0) // this is needed because if you have multiple store views, each individual store view will get "Use default value" unchecked for multiple attributes - which causes issues.
// ->save();
//}
//if ($product->getPrice() != $price) {
$status = trim($data['custom_stock_status']); //use corresponding attribute id in your csv file
//$price = trim($data['price']);
echo 'Getting product SKU: '.$sku.', with Custom Status: '.$product->getCustomStockStatus().'<br />\r\n';
echo 'Updating product SKU: '.$sku.', with Custom Status: '.$status.'<br />\r\n'; // .' and Price:'.$price.'<br />';
$product->setCustomStockStatus($status)
->setStoreId(0) // this is needed because if you have multiple store views, each individual store view will get "Use default value" unchecked for multiple attributes - which causes issues.
->save();
//}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////updating quantity/////////////////////////////////////////////////////////////////////
//try {
// $stockItem = $stockRegistry->getStockItemBySku($sku);
//}
//catch (\Exception $e) {
// $logger->info("Invalid stock for product SKU: ".$sku);
// continue;
//}
//if ($stockItem->getQty() != $qty) {
// $stockItem->setQty($qty);
// if ($qty > 0) {
// $stockItem->setIsInStock(1);
// }
// if ($qty < 1) {
// $stockItem->setIsInStock(0); //clean up first time
// }
// $stockRegistry->updateStockItemBySku($sku, $stockItem);
//}
}
fclose($file);
}
?>