製品をループして在庫数を更新しようとするコンソール コマンドを作成しました。完了後に(コマンドラインから)インデックスを再作成しましたが、変更されません。私は PHP7 がインストールされている vagrant box を使用しており、以前は save() メソッドに到達したときに「セグメンテーション違反」が発生していたため、管理セクションでインデクサーを「スケジュールに応じて更新」に変更し、コマンド ラインから手動でインデクサーを削除しましたが、まだ製品の数量が更新されていません。製品を適切に保存するために他に何かする必要がありますか?
<?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 as $x => $product) {
// $product->setData('qty', 100); also tried this, but it does 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>');
}
}