在 Magento 2 中以编程方式将新产品添加到现有购物车时,产品价格为 0

When adding new product programmatically to existing Cart in Magento 2, product price is 0

我的目标:

Magento 2.2.5

如果客户勾选了[添加购物袋]选项然后点击[继续购买],我会将[购物袋]产品添加到购物车并重定向到确认页面。

Cart page  → Confirm page


源代码:

public function addShoppingBag()
{
    $shoppingBagSku = $this->helper->getShoppingBagSku();
    $shoppingBagId = $this->productRepository->get($shoppingBagSku)->getId();
    $shoppingBagProduct = $this->productFactory->create()->load($shoppingBagId);
    $quote = $this->checkoutSession->getQuote();
    $params = array(
        'product' => $shoppingBagProduct->getId(),
        'qty' => 1,
        'price' => intval($shoppingBagProduct->getPrice())
    );

    $request = new \Magento\Framework\DataObject();
    $request->setData($params);
    $quote->addProduct($shoppingBagProduct, $request);
    $quote->getShippingAddress()->setCollectShippingRates(true);
    $this->quoteRepository->save($quote);
    $quote->collectTotals();
}

问题:

我检查了quote_item table,产品已添加,但与价格相关的所有属性均为 0。 quote_address_item table 可以,所有价格都是正确的。问题仅在于 quote_item.


我尝试过的东西

$this->cart->addProduct($shoppingBagProduct, $request);
$this->cart->save();
$this->cart->getQuote()->setTotalsCollectedFlag(false)->collectTotals()->save();

quote_item 价格会更新,但由于以下代码,它会再次重定向到购物车页面:

/magento2/source/vendor/magento/module-multishipping/Controller/Checkout.php

if ($this->_getCheckoutSession()->getCartWasUpdated(true)
    &&
    !in_array($action, ['index', 'login', 'register', 'addresses', 'success'])
) {
    $this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
    $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
    return parent::dispatch($request);
}

当我尝试:

setCartWasUpdated(false)

如我所愿重定向到确认页面,但quote_item价格仍然是0。

系统 > 配置 > 销售 > 结帐 > 添加产品后重定向到购物车设置为否


问题:

我在 google 中搜索了很多相同的问题,但没有找到我的目标。 可能是我在这里遗漏了一些东西,任何建议将不胜感激。 感谢您阅读我的问题。

正如我答应过你的,我会尽力在这里帮助你^^ 所以,首先,你应该明确地重构你的整个方法体。你不止一次地做了很多事情^^

我可以在这里向您展示一个以 magento 方式执行此操作的示例(未经测试 ^^)

<?php

class MyCustomAdd
{
    /**
     * @var \Magento\Quote\Api\Data\CartItemInterfaceFactory
     */
    protected $cartItemInterfaceFactory;
    /**
     * @var \Magento\Quote\Api\CartItemRepositoryInterface
     */
    protected $cartItemRepository;
    /**
     * I assume, that this is this class!
     * @var \Magento\Checkout\Model\Cart
     */
    protected $cart;

    public function __construct(
        \Magento\Quote\Api\Data\CartItemInterfaceFactory $cartItemInterfaceFactory,
        \Magento\Quote\Api\CartItemRepositoryInterface $cartItemRepository
    ) {
        $this->cartItemInterfaceFactory = $cartItemInterfaceFactory;
        $this->cartItemRepository = $cartItemRepository;
    }

    /**
     * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist.
     * @throws \Magento\Framework\Exception\CouldNotSaveException The specified item could not be saved to the cart.
     * @throws \Magento\Framework\Exception\InputException The specified item or cart is not valid.
     */
    public function addShoppingBagToCart()
    {
        $shippingBagSku = "coole_product_sku";
        $quoteId = $this->cart->getQuote()->getId();

        $cartItem = $this->cartItemInterfaceFactory->create(['data' => [
            \Magento\Quote\Api\Data\CartItemInterface::KEY_SKU      => $shippingBagSku,
            \Magento\Quote\Api\Data\CartItemInterface::KEY_QTY      => 1,
            \Magento\Quote\Api\Data\CartItemInterface::KEY_QUOTE_ID => $quoteId
        ]]);

        $this->cartItemRepository->save($cartItem);
    }
}

您可以像我在 Github 问题中那样实现我对 CartItemRepository 的自定义修复。

您好, 乌尔夫

我需要在添加产品之前设置 multishipping = false。

$quote->setIsMultiShipping(false);
$quote->addProduct($this->getShoppingBagProduct(), $quantity);