Magento 2 CRUD ORM – Fix NoSuchEntityException When Updating Products

crudmagento2orm

I am Trying to update product programmatically magento2 Magento ver. 2.1.2

Hear is my code

require 'vendor/autoload.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');
$product = $objectManager->get('Magento\Catalog\Model\Product')->load('31');
$product->setName('Test Product');
$product->save();

but I can Load product by Id without any issue.
only can't update

here is detailed error

#0 /home/public_html/shop/vendor/magento/module-store/Model/StoreManager.php(198): Magento\Store\Model\WebsiteRepository->getById('12')
#1 /home/public_html/shop/vendor/magento/module-catalog/Model/Product.php(786): Magento\Store\Model\StoreManager->getWebsite('12')
#2 /home/public_html/shop/var/generation/Magento/Catalog/Model/Product/Interceptor.php(310): Magento\Catalog\Model\Product->getStoreIds()
#3 /home/public_html/shop/vendor/magento/module-catalog/Model/Product/Gallery/UpdateHandler.php(74): Magento\Catalog\Model\Product\Interceptor->getStoreIds()
#4 /home/public_html/shop/vendor/magento/module-catalog/Model/Product/Gallery/UpdateHandler.php(24): Magento\Catalog\Model\Product\Gallery\UpdateHandler->extractStoreIds(Object(Magento\Catalog\Model\Product\Interceptor))
#5 /home/public_html/shop/vendor/magento/module-catalog/Model/Product/Gallery/CreateHandler.php(182): Magento\Catalog\Model\Product\Gallery\UpdateHandler->processDeletedImages(Object(Magento\Catalog\Model\Product\Interceptor), Array)
#6 /home/public_html/shop/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Catalog\Model\Product\Gallery\CreateHandler->execute(Object(Magento\Catalog\Model\Product\Interceptor), Array)
#7 /home/public_html/shop/var/generation/Magento/Catalog/Model/Product/Gallery/UpdateHandler/Interceptor.php(26): Magento\Catalog\Model\Product\Gallery\UpdateHandler\Interceptor->___callPlugins('execute', Array, Array)
#8 /home/public_html/shop/vendor/magento/framework/EntityManager/Operation/Update/UpdateExtensions.php(49): Magento\Catalog\Model\Product\Gallery\UpdateHandler\Interceptor->execute(Object(Magento\Catalog\Model\Product\Interceptor), Array)
#9 /home/public_html/shop/vendor/magento/framework/EntityManager/Operation/Update.php(107): Magento\Framework\EntityManager\Operation\Update\UpdateExtensions->execute(Object(Magento\Catalog\Model\Product\Interceptor), Array)
#10 /home/public_html/shop/vendor/magento/framework/EntityManager/EntityManager.php(87): Magento\Framework\EntityManager\Operation\Update->execute(Object(Magento\Catalog\Model\Product\Interceptor), Array)
#11 /home/public_html/shop/vendor/magento/module-catalog/Model/ResourceModel/Product.php(695): Magento\Framework\EntityManager\EntityManager->save(Object(Magento\Catalog\Model\Product\Interceptor))
#12 /home/public_html/shop/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Catalog\Model\ResourceModel\Product->save(Object(Magento\Catalog\Model\Product\Interceptor))
#13 /home/public_html/shop/vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Catalog\Model\ResourceModel\Product\Interceptor->___callParent('save', Array)
#14 /home/public_html/shop/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Catalog...', 'save', Object(Magento\Catalog\Model\ResourceModel\Product\Interceptor), Array, 'catalogsearchFu...')
#15 /home/public_html/shop/vendor/magento/module-catalog-search/Model/Indexer/Fulltext/Plugin/Product.php(51): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Catalog\Model\Product\Interceptor))
#16 /home/public_html/shop/vendor/magento/module-catalog-search/Model/Indexer/Fulltext/Plugin/Product.php(24): Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product->addCommitCallback(Object(Magento\Catalog\Model\ResourceModel\Product\Interceptor), Object(Closure), Object(Magento\Catalog\Model\Product\Interceptor))
#17 /home/public_html/shop/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product->aroundSave(Object(Magento\Catalog\Model\ResourceModel\Product\Interceptor), Object(Closure), Object(Magento\Catalog\Model\Product\Interceptor))
#18 /home/public_html/shop/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Catalog...', 'save', Object(Magento\Catalog\Model\ResourceModel\Product\Interceptor), Array, 'clean_cache')
#19 /home/public_html/shop/vendor/magento/framework/App/Cache/FlushCacheByTags.php(60): Magento\Catalog\Model\ResourceModel\Product\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Catalog\Model\Product\Interceptor))
#20 /home/public_html/shop/vendor/magento/framework/Interception/Interceptor.php(142): Magento\Framework\App\Cache\FlushCacheByTags->aroundSave(Object(Magento\Catalog\Model\ResourceModel\Product\Interceptor), Object(Closure), Object(Magento\Catalog\Model\Product\Interceptor))
#21 /home/public_html/shop/var/generation/Magento/Catalog/Model/ResourceModel/Product/Interceptor.php(273): Magento\Catalog\Model\ResourceModel\Product\Interceptor->___callPlugins('save', Array, Array)
#22 /home/public_html/shop/vendor/magento/framework/Model/AbstractModel.php(631): Magento\Catalog\Model\ResourceModel\Product\Interceptor->save(Object(Magento\Catalog\Model\Product\Interceptor))
#23 /home/public_html/shop/var/generation/Magento/Catalog/Model/Product/Interceptor.php(2364): Magento\Framework\Model\AbstractModel->save()
#24 /home/public_html/maybray/index.php(409): Magento\Catalog\Model\Product\Interceptor->save()
#25 [internal function]: {closure}()

Best Answer

Use below code:

<?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');
$product = $objectManager->get('Magento\Catalog\Model\Product')->load('5');
$product->setName('best prod');
$product->setStoreId(); //your store id here
$product->setWebsiteIds(array(1)); // Product under websites     
$product->save();
Related Topic