[英]Magento 2 When Adding a product to cart programmatically , mini cart product price become zero($0.00)
[英]When adding new product programmatically to existing Cart in Magento 2, product price is 0
我的目標:
Magento 2.2.5
如果客戶選中[添加購物袋]選項,然后單擊[繼續購買],我會將[購物袋]產品添加到購物車並重定向到確認頁面。
源代碼:
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。
系統>配置>銷售>結帳>將產品重定向添加到購物車后設置為否
題:
我在Google中搜索了很多相同的問題,但是沒有運氣來歸檔我的目標。 可能是我在這里缺少什么,任何建議將不勝感激。 感謝您閱讀我的問題。
添加產品之前,我需要設置multishiping = false。
$quote->setIsMultiShipping(false);
$quote->addProduct($this->getShoppingBagProduct(), $quantity);
正如我向您保證的那樣,我會盡力為您提供幫助^^因此,首先,您應該明確地重構您的整個方法主體。 您不止一次做很多事情^^
我可以在這里向您展示以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 Issue中那樣為CartItemRepository實施我的自定義修復程序。
烏爾夫·格雷茲
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.