php - Magento 2: Can't update product stock (quantity) -
i created console command loops through products , attempts update stock quantity. reindex (from command line) after it's completed never changes. on vagrant box has php7 install, , getting "segmentation fault" when got save() method, had change indexer "update on schedule" in admin section , run indexer command line manually, i'm still not seeing product quantity update. there else need in order product save properly?
<?php namespace myapp\productupdate\console\command; use symfony\component\console\command\command; use symfony\component\console\input\inputinterface; use symfony\component\console\output\outputinterface; use magento\catalog\api\data\productinterface; use magento\catalog\api\productrepositoryinterface; use magento\framework\api\filterbuilder; use magento\framework\api\searchcriteriabuilder; use magento\catalog\model\product\interceptor; use magento\framework\app\state; use magento\framework\app\objectmanager\configloader; use magento\framework\objectmanagerinterface; use magento\framework\registry; class updatecommand extends command { /** * @var productrepositoryinterface */ private $productrepo; /** * @var searchcriteriabuilder */ private $searchcriteriabuilder; /** * @var filterbuilder */ private $filterbuilder; /** * @var state */ private $state; /** * @var objectmanagerinterface */ private $objectmanager; /** * @var registry */ private $registry; /** * @var configloader */ private $configloader; /** * create new update command instance. * * @param productrepositoryinterface $productrepo [description] * @param searchcriteriabuilder $searchcriteriabuilder [description] * @param filterbuilder $filterbuilder [description] * @param state $state [description] * @param objectmanagerinterface $objectmanager [description] * @param registry $registry [description] */ public function __construct( productrepositoryinterface $productrepo, searchcriteriabuilder $searchcriteriabuilder, filterbuilder $filterbuilder, state $state, objectmanagerinterface $objectmanager, registry $registry, configloader $configloader ) { $this->productrepo = $productrepo; $this->searchcriteriabuilder = $searchcriteriabuilder; $this->filterbuilder = $filterbuilder; $this->state = $state; $this->objectmanager = $objectmanager; $this->registry = $registry; $this->configloader = $configloader; parent::__construct(); } /** * {@inheritdoc} */ protected function configure() { $this->setname('myapp:inventory:update') ->setdescription('updates product quantities.'); parent::configure(); } /** * {@inheritdoc} */ protected function execute(inputinterface $input, outputinterface $output) { $this->state->setareacode('adminhtml'); $this->objectmanager->configure($this->configloader->load('adminhtml')); $this->registry->register('issecurearea', true); $continue = true; $currentpage = 1; $currentproduct = 1; $perpage = 1000; $output->writeln('<info>getting list of products:<info>'); while ($continue) { $searchcriteria = $this->searchcriteriabuilder ->setpagesize(1000) ->setcurrentpage(1) ->create(); $results = $this->productrepo->getlist($searchcriteria); if ($results->gettotalcount() == 0) { $continue = false; continue; } $products = $results->getitems(); foreach ($products $x => $product) { // $product->setdata('qty', 100); tried this, not work. $product->setqty(100); $product->sethasdatachanges(true); $product->save(); $output->writeln('<info>updated product: '. $product->getsku() .' | number: ' . $currentproduct . '</info>'); $currentproduct++; } $currentpage++; } $output->writeln('<info>updating complete!</info>'); } }
try code this:
$product->setstockdata(['qty' => $qty, 'is_in_stock' => 1]); $product->setquantityandstockstatus(['qty' => $qty, 'is_in_stock' => 1]);
it working me. setqty not work since quantity not directly stored in product eav table, uses separate stock_item table
Comments
Post a Comment