Product Attribute – How to Update from Magento 2 Root Folder

attributescustomer-attributemagento2product-attribute

How to update product attribute from magento 2 root folder ?

I want to add file on magento root and run this file to update product,customer or category attribute from magento root

Best Answer

Here is example for update product attribute

<?php 
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$obj->get('\Magento\Framework\App\State')->setAreaCode('frontend');

$collection = $obj->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $collection->create();
$collection->addFieldToSelect('*');
foreach ($collection->getItems() as $product)
{
    $product->setData('new',1); // attribute code and value
    $product->save();
}
Related Topic