代码之家  ›  专栏  ›  技术社区  ›  dubloons

为什么第二个产品会被动态添加到购物车中?它在Magento2中是可选的。

  •  6
  • dubloons  · 技术社区  · 7 年前

    我正在用一些自定义选项动态地将产品添加到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 表。

    任何关于为什么或如何修复的想法都会受到赞赏。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Script47    7 年前

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

    最后 true forceReload .

    推荐文章