0

我目前正在研究 magento2.3 MSI。我已经安装了 MSI,现在我正在尝试处理它。我创建了 3 个来源和 1 个库存。我需要将库存分配给来源。目前我已经创建了用于导入库存的 csv 导入器,但在此过程中,我还想将源名称分配给现有产品。例如:我有 source1、source2 和 source3 之类的来源,我不仅要分配更新数量和库存状态,还要分配源名称。

我用过这段代码:

$product->setStockData(['qty' => 4,'manage_stock' => 1,'source_code' => 'source1','name' => 'My source 1','is_in_stock' => 1]);

$product->setQuantityAndStockStatus(['qty' => 4,'manage_stock' => 1,'source_code' => 'source1','name' => 'My source 1','is_in_stock' => 1]);
$product->save();

数量和 is_in_stock 运行良好,但无法更新源名称以分配给该产品。这段代码有什么问题吗?或者有没有其他方法可以解决这个问题。如果有人知道解决方案,那将是非常可观的。谢谢!

这是我的模型源代码:

public function execute(array $data)
{

    /**
     * @var Product $product
     */
    $product = $this->productRepository->get($data[self::CSV_SKU]);

    /** @var StockItemInterface $stockItem */
    $stockItem = $this->stockRegistory->getStockItem($product->getId());

    $attributes = [];

    $product->setStockData(
        [
            'qty' => 4,
            'stock_id' => 1,
            'manage_stock' => 1,
            'source_code' => 'hatagaya_store',
            'name' => 'Bluelug Hatagaya store',
            'is_in_stock' => 1
        ]
    );
    $product->setQuantityAndStockStatus(
        [
            'qty' => 4,
            'stock_id' => 1,
            'manage_stock' => 1,
            'source_code' => 'hatagaya_store',
            'name' => 'Bluelug Hatagaya store',
            'is_in_stock' => 1
        ]
    );
    $product->save();

    $stockItem->setUseConfigManageStock(1);

    $this->stockItemRepository->save($stockItem);
    $this->action->updateAttributes([$product->getId()], $attributes, 0);

    return $product;
}

预期结果是将源分配给该产品,如下图所示:

在此处输入图像描述

但源未分配给该产品。

4

1 回答 1

0

这是一种将数量设置为特定库存来源的方法:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$sourceItem1 = $objectManager->create('Magento\InventoryApi\Api\Data\SourceItemInterface');

$sourceItem1->setSku('product_sku1');
$sourceItem1->setSourceCode('inventory_source_code1');
$sourceItem1->setQuantity(10);
$sourceItem1->setStatus(1);

$sourceItem2 = $objectManager->create('Magento\InventoryApi\Api\Data\SourceItemInterface');
$sourceItem2->setSku('product_sku1');
$sourceItem2->setSourceCode('inventory_source_code2');
$sourceItem2->setQuantity(20);
$sourceItem2->setStatus(1);


$sourceItemSave = $objectManager->get('\Magento\InventoryApi\Api\SourceItemsSaveInterface');
$sourceItemSave->execute([ $sourceItem1, $sourceItem2]);
于 2021-08-26T11:42:34.080 回答