Magento 2 PHP – Update Product Details Using SKU

magento2PHP

How to Update product details using sku via script??
for example update description , short desc, price etc.. for a configurable product or a simple product

Best Answer

You need to create folder in root of your system and create one php file inside those folder,

Keep below content in your file and run from browser,

<?php
// MAGENTO START
include('app/bootstrap.php');
use Magento\Framework\App\Bootstrap;
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('sku', '24-MB01');
$collection->addAttributeToSelect('*');
foreach ($collection as $product) 
{
    $product->setData('short_description', 'test description');
    $product->save();
    echo "Product Updated". " ";
} 
Related Topic