簡體   English   中英

Magento 2:無法更新產品庫存(數量)

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

我創建了一個控制台命令,該命令循環遍歷產品並嘗試更新庫存數量。 我完成索引后(從命令行)重新索引,但它從未更改。 我在一個已經安裝了PHP7的無聊的盒子上,並且當我使用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>');
    }
}

嘗試這樣的代碼:

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

它為我工作。 setQty不起作用,因為數量沒有直接存儲在產品EAV表中,它使用單獨的stock_item表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM