3

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}()
  • Code is working fine for me can you please provide full code – Prashant Valanda Dec 22 '16 at 5:03
  • that is my full code 31 is product id.I have multiple stores.I run this externally.I need to add others but testing I update only name – srinath madusanka Dec 22 '16 at 5:15
1

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();
  • thank you very much .$product->setWebsiteIds(array(1)); is the missing parts.I don't know why it's necessary – srinath madusanka Dec 22 '16 at 5:40
0

The accepted answer is definitely NOT the way to go, you should not use the Objectmanager to instantiate an object.

Instead, use the repository (\Magento\Catalog\Model\ProductRepository):

$product = $this->productRepository->getById(5);
$product->setName("best prod");
$product->setStoreId();
$product->setWebsiteIds(array(1));
$product->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.