Magento2 Product Update – How to Update Product Using Custom File

databasemagento2productproductsscript

How to update product using the custom file from Magento root.?

https://www.domain.com/update.php

Best Answer

For update product using the custom file from Magento root, first of all, you need to create update.php a file at your Magento root.

And then paste following code into your file and then run `https://www.domain.com/update.php

<?php

use Magento\Framework\App\Bootstrap;

include 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

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

$productId = 1;
$product = $objectManager->create('Magento\Catalog\Model\Product');
$product->load($productId);
$product->setName('Enter Your Product Name Here');
$product->save();

On above example, we have just update product name.

After running this script if you cache is enabled then you need to refresh your cache using the following command.

php bin/magento cache:clean

Hope that'll help you.

Related Topic