我正在使用一些自定义选项将产品动态添加到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
表中有一个条目。
关于为什么或如何修复的任何想法将不胜感激。
在每次添加之间强制重新加载产品可解决此问题。
$this->productRepository->get('simple-product-1', false, null, true);
最后一个true
参数是forceReload
。