为什么动态添加到购物车的第二个产品在 Magento 2 中失去其选项

Why does the second product dynamically added to the cart loose it's options in Magento2

我正在使用一些自定义选项将产品动态添加到 Magento2 中的购物车。每个产品都有相同的基本产品 ID,但选项不同。 Represent Product 已被正确覆盖,因此添加到购物车的所有产品都是独立的。但是使用此代码,添加的第二个产品将失去其自定义选项:

$magento_product = $this->productRepository->get('simple-product-1');
$params = array(
    'product' => $magento_product->getId(),
    'qty'     => intval(5),
    'options' => array(
        'cr_price' => 12.0,
        'Product' => "Test P",
        'cr_XML' => '<root></root>'
    ),
);
$this->cart->addProduct($magento_product, $params);
$params = array(
    'product' => $magento_product->getId(),
    'qty'     => intval(10),
    'options' => array(
        'cr_price' => 14.0,
        'Product' => "Test P2",
        'cr_XML' => '<root></root>'
    ),
);
$this->cart->addProduct($magento_product, $params);
$this->cart->save();

只有第一个产品在 quote_item_option table 中有条目。

任何关于为什么或如何修复的想法将不胜感激。

在每次添加之间强制重新加载产品修复了这个问题。

$this->productRepository->get('simple-product-1', false, null, true);

最后一个true参数是forceReload