简体   繁体   English

Magento 2:无法更新产品库存(数量)

[英]Magento 2: Can't update product stock (quantity)

I created a console command that loops through the products and attempts to update the stock quantity. 我创建了一个控制台命令,该命令循环遍历产品并尝试更新库存数量。 I reindex (from the command line) after it's completed but it never changes. 我完成索引后(从命令行)重新索引,但它从未更改。 I am on a vagrant box that has PHP7 install, and was previously getting a "Segmentation Fault" when I got to the save() method, so I had to change the indexer to "Update on Schedule" in the admin section and run the indexer from the command line manually, but I'm still not seeing the product quantity update. 我在一个已经安装了PHP7的无聊的盒子上,并且当我使用save()方法时以前遇到了“分段错误”,因此我不得不在管理部分中将索引器更改为“按计划更新”,然后运行从命令行手动建立索引器,但是我仍然没有看到产品数量的更新。 Is there something else I need to do in order to get the product to 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 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>');
    }
}

try some code like this: 尝试这样的代码:

$product->setStockData(['qty' => $qty, 'is_in_stock' => 1]);
$product->setQuantityAndStockStatus(['qty' => $qty, 'is_in_stock' => 1]);

it is working for me. 它为我工作。 setQty will not work since quantity is not directly stored in the product EAV table, it uses a separate stock_item table setQty不起作用,因为数量没有直接存储在产品EAV表中,它使用单独的stock_item表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM