我创建了一个控制台命令,循环浏览产品并尝试更新库存数量。我在完成后重新索引(从命令行),但它永远不会改变。我在一个安装了PHP7的流浪盒上,当我进入save()方法时,之前遇到了“Segmentation Fault”,所以我不得不在admin部分将索引器更改为“Update on Schedule”并运行手动从命令行索引器,但我仍然没有看到产品数量更新。为了让产品得到妥善保存,我还需要做些什么吗?
<?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>');
}
}
答案 0 :(得分:0)
尝试这样的代码:
$product->setStockData(['qty' => $qty, 'is_in_stock' => 1]);
$product->setQuantityAndStockStatus(['qty' => $qty, 'is_in_stock' => 1]);
这对我有用。 setQty不起作用,因为数量没有直接存储在产品EAV表中,它使用单独的stock_item表