【问题标题】:When adding new product programmatically to existing Cart in Magento 2, product price is 0在 Magento 2 中以编程方式将新产品添加到现有购物车时,产品价格为 0
【发布时间】:2018-11-07 10:37:44
【问题描述】:

我的目标:

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 表,添加了产品,但与价格相关的所有属性均为 0。 quote_address_item 表很好,所有价格都正确。问题只出在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。

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


问题:

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

【问题讨论】:

    标签: php magento magento2


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      我可以在这里向您展示一个以 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 实施我的自定义修复。

      问候, 乌尔夫

      【讨论】:

      • 它不起作用,当我调试时,送货地址只包含 1 个产品shippingAddress-&gt;getAllItems() → return 1,购物产品不在那里。所以我认为这是个问题。我尝试像这样将项目添加到地址:shippingAddress-&gt;addItem($item) 但它不起作用。
      猜你喜欢
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 2017-05-18
      相关资源
      最近更新 更多