When adding new product programmatically to existing Cart in Magento 2, product price is 0
up vote
0
down vote
favorite
My goal:
Magento 2.2.5
If the customer checked to [Add Shopping Bag] option then click [Proceed to Purchase], I'll add the [Shopping Bag] product to cart and redirect to confirm page.
Cart page → Confirm page
Source Code:
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 MagentoFrameworkDataObject();
$request->setData($params);
$quote->addProduct($shoppingBagProduct, $request);
$quote->getShippingAddress()->setCollectShippingRates(true);
$this->quoteRepository->save($quote);
$quote->collectTotals();
}
Problem:
I checked quote_item table, product was added but all attributes related to price are 0.
quote_address_item table is fine, all prices are correct. The problem is only with quote_item.
The things I tried
$this->cart->addProduct($shoppingBagProduct, $request);
$this->cart->save();
$this->cart->getQuote()->setTotalsCollectedFlag(false)->collectTotals()->save();
The quote_item price will be updated, but it will redirect to Cart page again because of the following code:
/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);
}
When I try to:
setCartWasUpdated(false)
It redirects to Confirm page as I want, but the quote_item price is still 0.
System > Configuration > Sales > Checkout > After Adding a Product Redirect to Shopping Cart is set to No
Question:
I searched a lot the same problem in google but no luck to archive my goal.
May be I'm missing something here, any suggestion will be appreciated.
Thank you for reading my problem.
php magento magento2
add a comment |
up vote
0
down vote
favorite
My goal:
Magento 2.2.5
If the customer checked to [Add Shopping Bag] option then click [Proceed to Purchase], I'll add the [Shopping Bag] product to cart and redirect to confirm page.
Cart page → Confirm page
Source Code:
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 MagentoFrameworkDataObject();
$request->setData($params);
$quote->addProduct($shoppingBagProduct, $request);
$quote->getShippingAddress()->setCollectShippingRates(true);
$this->quoteRepository->save($quote);
$quote->collectTotals();
}
Problem:
I checked quote_item table, product was added but all attributes related to price are 0.
quote_address_item table is fine, all prices are correct. The problem is only with quote_item.
The things I tried
$this->cart->addProduct($shoppingBagProduct, $request);
$this->cart->save();
$this->cart->getQuote()->setTotalsCollectedFlag(false)->collectTotals()->save();
The quote_item price will be updated, but it will redirect to Cart page again because of the following code:
/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);
}
When I try to:
setCartWasUpdated(false)
It redirects to Confirm page as I want, but the quote_item price is still 0.
System > Configuration > Sales > Checkout > After Adding a Product Redirect to Shopping Cart is set to No
Question:
I searched a lot the same problem in google but no luck to archive my goal.
May be I'm missing something here, any suggestion will be appreciated.
Thank you for reading my problem.
php magento magento2
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
My goal:
Magento 2.2.5
If the customer checked to [Add Shopping Bag] option then click [Proceed to Purchase], I'll add the [Shopping Bag] product to cart and redirect to confirm page.
Cart page → Confirm page
Source Code:
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 MagentoFrameworkDataObject();
$request->setData($params);
$quote->addProduct($shoppingBagProduct, $request);
$quote->getShippingAddress()->setCollectShippingRates(true);
$this->quoteRepository->save($quote);
$quote->collectTotals();
}
Problem:
I checked quote_item table, product was added but all attributes related to price are 0.
quote_address_item table is fine, all prices are correct. The problem is only with quote_item.
The things I tried
$this->cart->addProduct($shoppingBagProduct, $request);
$this->cart->save();
$this->cart->getQuote()->setTotalsCollectedFlag(false)->collectTotals()->save();
The quote_item price will be updated, but it will redirect to Cart page again because of the following code:
/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);
}
When I try to:
setCartWasUpdated(false)
It redirects to Confirm page as I want, but the quote_item price is still 0.
System > Configuration > Sales > Checkout > After Adding a Product Redirect to Shopping Cart is set to No
Question:
I searched a lot the same problem in google but no luck to archive my goal.
May be I'm missing something here, any suggestion will be appreciated.
Thank you for reading my problem.
php magento magento2
My goal:
Magento 2.2.5
If the customer checked to [Add Shopping Bag] option then click [Proceed to Purchase], I'll add the [Shopping Bag] product to cart and redirect to confirm page.
Cart page → Confirm page
Source Code:
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 MagentoFrameworkDataObject();
$request->setData($params);
$quote->addProduct($shoppingBagProduct, $request);
$quote->getShippingAddress()->setCollectShippingRates(true);
$this->quoteRepository->save($quote);
$quote->collectTotals();
}
Problem:
I checked quote_item table, product was added but all attributes related to price are 0.
quote_address_item table is fine, all prices are correct. The problem is only with quote_item.
The things I tried
$this->cart->addProduct($shoppingBagProduct, $request);
$this->cart->save();
$this->cart->getQuote()->setTotalsCollectedFlag(false)->collectTotals()->save();
The quote_item price will be updated, but it will redirect to Cart page again because of the following code:
/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);
}
When I try to:
setCartWasUpdated(false)
It redirects to Confirm page as I want, but the quote_item price is still 0.
System > Configuration > Sales > Checkout > After Adding a Product Redirect to Shopping Cart is set to No
Question:
I searched a lot the same problem in google but no luck to archive my goal.
May be I'm missing something here, any suggestion will be appreciated.
Thank you for reading my problem.
php magento magento2
php magento magento2
edited Nov 7 at 11:40
SebaGra
831925
831925
asked Nov 7 at 10:37
HoangTuan
11
11
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
I need to set multishiping = false before add product.
$quote->setIsMultiShipping(false);
$quote->addProduct($this->getShoppingBagProduct(), $quantity);
add a comment |
up vote
-1
down vote
as i promised to you, I'll try to help you here this way ^^
So, first of all, you should definitly refactor you're whole method body. You're doing a lot of stuff more than once ^^
I can show you here an example of doing it in a magento way (untested ^^)
<?php
class MyCustomAdd
{
/**
* @var MagentoQuoteApiDataCartItemInterfaceFactory
*/
protected $cartItemInterfaceFactory;
/**
* @var MagentoQuoteApiCartItemRepositoryInterface
*/
protected $cartItemRepository;
/**
* I assume, that this is this class!
* @var MagentoCheckoutModelCart
*/
protected $cart;
public function __construct(
MagentoQuoteApiDataCartItemInterfaceFactory $cartItemInterfaceFactory,
MagentoQuoteApiCartItemRepositoryInterface $cartItemRepository
) {
$this->cartItemInterfaceFactory = $cartItemInterfaceFactory;
$this->cartItemRepository = $cartItemRepository;
}
/**
* @throws MagentoFrameworkExceptionNoSuchEntityException The specified cart does not exist.
* @throws MagentoFrameworkExceptionCouldNotSaveException The specified item could not be saved to the cart.
* @throws MagentoFrameworkExceptionInputException 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' => [
MagentoQuoteApiDataCartItemInterface::KEY_SKU => $shippingBagSku,
MagentoQuoteApiDataCartItemInterface::KEY_QTY => 1,
MagentoQuoteApiDataCartItemInterface::KEY_QUOTE_ID => $quoteId
]]);
$this->cartItemRepository->save($cartItem);
}
}
You may implement my custom fix for the CartItemRepository like i did it in the Github Issue.
Greetz,
Ulf
It is not working, when I debug, the shipping address only contain 1 productshippingAddress->getAllItems()→ return 1 , the shopping product is not there. So I think that is problem. I tried to add item to address like this way:shippingAddress->addItem($item)but it not working.
– HoangTuan
Nov 8 at 2:58
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
I need to set multishiping = false before add product.
$quote->setIsMultiShipping(false);
$quote->addProduct($this->getShoppingBagProduct(), $quantity);
add a comment |
up vote
0
down vote
accepted
I need to set multishiping = false before add product.
$quote->setIsMultiShipping(false);
$quote->addProduct($this->getShoppingBagProduct(), $quantity);
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I need to set multishiping = false before add product.
$quote->setIsMultiShipping(false);
$quote->addProduct($this->getShoppingBagProduct(), $quantity);
I need to set multishiping = false before add product.
$quote->setIsMultiShipping(false);
$quote->addProduct($this->getShoppingBagProduct(), $quantity);
answered Nov 15 at 5:08
HoangTuan
11
11
add a comment |
add a comment |
up vote
-1
down vote
as i promised to you, I'll try to help you here this way ^^
So, first of all, you should definitly refactor you're whole method body. You're doing a lot of stuff more than once ^^
I can show you here an example of doing it in a magento way (untested ^^)
<?php
class MyCustomAdd
{
/**
* @var MagentoQuoteApiDataCartItemInterfaceFactory
*/
protected $cartItemInterfaceFactory;
/**
* @var MagentoQuoteApiCartItemRepositoryInterface
*/
protected $cartItemRepository;
/**
* I assume, that this is this class!
* @var MagentoCheckoutModelCart
*/
protected $cart;
public function __construct(
MagentoQuoteApiDataCartItemInterfaceFactory $cartItemInterfaceFactory,
MagentoQuoteApiCartItemRepositoryInterface $cartItemRepository
) {
$this->cartItemInterfaceFactory = $cartItemInterfaceFactory;
$this->cartItemRepository = $cartItemRepository;
}
/**
* @throws MagentoFrameworkExceptionNoSuchEntityException The specified cart does not exist.
* @throws MagentoFrameworkExceptionCouldNotSaveException The specified item could not be saved to the cart.
* @throws MagentoFrameworkExceptionInputException 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' => [
MagentoQuoteApiDataCartItemInterface::KEY_SKU => $shippingBagSku,
MagentoQuoteApiDataCartItemInterface::KEY_QTY => 1,
MagentoQuoteApiDataCartItemInterface::KEY_QUOTE_ID => $quoteId
]]);
$this->cartItemRepository->save($cartItem);
}
}
You may implement my custom fix for the CartItemRepository like i did it in the Github Issue.
Greetz,
Ulf
It is not working, when I debug, the shipping address only contain 1 productshippingAddress->getAllItems()→ return 1 , the shopping product is not there. So I think that is problem. I tried to add item to address like this way:shippingAddress->addItem($item)but it not working.
– HoangTuan
Nov 8 at 2:58
add a comment |
up vote
-1
down vote
as i promised to you, I'll try to help you here this way ^^
So, first of all, you should definitly refactor you're whole method body. You're doing a lot of stuff more than once ^^
I can show you here an example of doing it in a magento way (untested ^^)
<?php
class MyCustomAdd
{
/**
* @var MagentoQuoteApiDataCartItemInterfaceFactory
*/
protected $cartItemInterfaceFactory;
/**
* @var MagentoQuoteApiCartItemRepositoryInterface
*/
protected $cartItemRepository;
/**
* I assume, that this is this class!
* @var MagentoCheckoutModelCart
*/
protected $cart;
public function __construct(
MagentoQuoteApiDataCartItemInterfaceFactory $cartItemInterfaceFactory,
MagentoQuoteApiCartItemRepositoryInterface $cartItemRepository
) {
$this->cartItemInterfaceFactory = $cartItemInterfaceFactory;
$this->cartItemRepository = $cartItemRepository;
}
/**
* @throws MagentoFrameworkExceptionNoSuchEntityException The specified cart does not exist.
* @throws MagentoFrameworkExceptionCouldNotSaveException The specified item could not be saved to the cart.
* @throws MagentoFrameworkExceptionInputException 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' => [
MagentoQuoteApiDataCartItemInterface::KEY_SKU => $shippingBagSku,
MagentoQuoteApiDataCartItemInterface::KEY_QTY => 1,
MagentoQuoteApiDataCartItemInterface::KEY_QUOTE_ID => $quoteId
]]);
$this->cartItemRepository->save($cartItem);
}
}
You may implement my custom fix for the CartItemRepository like i did it in the Github Issue.
Greetz,
Ulf
It is not working, when I debug, the shipping address only contain 1 productshippingAddress->getAllItems()→ return 1 , the shopping product is not there. So I think that is problem. I tried to add item to address like this way:shippingAddress->addItem($item)but it not working.
– HoangTuan
Nov 8 at 2:58
add a comment |
up vote
-1
down vote
up vote
-1
down vote
as i promised to you, I'll try to help you here this way ^^
So, first of all, you should definitly refactor you're whole method body. You're doing a lot of stuff more than once ^^
I can show you here an example of doing it in a magento way (untested ^^)
<?php
class MyCustomAdd
{
/**
* @var MagentoQuoteApiDataCartItemInterfaceFactory
*/
protected $cartItemInterfaceFactory;
/**
* @var MagentoQuoteApiCartItemRepositoryInterface
*/
protected $cartItemRepository;
/**
* I assume, that this is this class!
* @var MagentoCheckoutModelCart
*/
protected $cart;
public function __construct(
MagentoQuoteApiDataCartItemInterfaceFactory $cartItemInterfaceFactory,
MagentoQuoteApiCartItemRepositoryInterface $cartItemRepository
) {
$this->cartItemInterfaceFactory = $cartItemInterfaceFactory;
$this->cartItemRepository = $cartItemRepository;
}
/**
* @throws MagentoFrameworkExceptionNoSuchEntityException The specified cart does not exist.
* @throws MagentoFrameworkExceptionCouldNotSaveException The specified item could not be saved to the cart.
* @throws MagentoFrameworkExceptionInputException 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' => [
MagentoQuoteApiDataCartItemInterface::KEY_SKU => $shippingBagSku,
MagentoQuoteApiDataCartItemInterface::KEY_QTY => 1,
MagentoQuoteApiDataCartItemInterface::KEY_QUOTE_ID => $quoteId
]]);
$this->cartItemRepository->save($cartItem);
}
}
You may implement my custom fix for the CartItemRepository like i did it in the Github Issue.
Greetz,
Ulf
as i promised to you, I'll try to help you here this way ^^
So, first of all, you should definitly refactor you're whole method body. You're doing a lot of stuff more than once ^^
I can show you here an example of doing it in a magento way (untested ^^)
<?php
class MyCustomAdd
{
/**
* @var MagentoQuoteApiDataCartItemInterfaceFactory
*/
protected $cartItemInterfaceFactory;
/**
* @var MagentoQuoteApiCartItemRepositoryInterface
*/
protected $cartItemRepository;
/**
* I assume, that this is this class!
* @var MagentoCheckoutModelCart
*/
protected $cart;
public function __construct(
MagentoQuoteApiDataCartItemInterfaceFactory $cartItemInterfaceFactory,
MagentoQuoteApiCartItemRepositoryInterface $cartItemRepository
) {
$this->cartItemInterfaceFactory = $cartItemInterfaceFactory;
$this->cartItemRepository = $cartItemRepository;
}
/**
* @throws MagentoFrameworkExceptionNoSuchEntityException The specified cart does not exist.
* @throws MagentoFrameworkExceptionCouldNotSaveException The specified item could not be saved to the cart.
* @throws MagentoFrameworkExceptionInputException 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' => [
MagentoQuoteApiDataCartItemInterface::KEY_SKU => $shippingBagSku,
MagentoQuoteApiDataCartItemInterface::KEY_QTY => 1,
MagentoQuoteApiDataCartItemInterface::KEY_QUOTE_ID => $quoteId
]]);
$this->cartItemRepository->save($cartItem);
}
}
You may implement my custom fix for the CartItemRepository like i did it in the Github Issue.
Greetz,
Ulf
answered Nov 7 at 17:21
Ulf Tietze
11
11
It is not working, when I debug, the shipping address only contain 1 productshippingAddress->getAllItems()→ return 1 , the shopping product is not there. So I think that is problem. I tried to add item to address like this way:shippingAddress->addItem($item)but it not working.
– HoangTuan
Nov 8 at 2:58
add a comment |
It is not working, when I debug, the shipping address only contain 1 productshippingAddress->getAllItems()→ return 1 , the shopping product is not there. So I think that is problem. I tried to add item to address like this way:shippingAddress->addItem($item)but it not working.
– HoangTuan
Nov 8 at 2:58
It is not working, when I debug, the shipping address only contain 1 product
shippingAddress->getAllItems() → return 1 , the shopping product is not there. So I think that is problem. I tried to add item to address like this way: shippingAddress->addItem($item) but it not working.– HoangTuan
Nov 8 at 2:58
It is not working, when I debug, the shipping address only contain 1 product
shippingAddress->getAllItems() → return 1 , the shopping product is not there. So I think that is problem. I tried to add item to address like this way: shippingAddress->addItem($item) but it not working.– HoangTuan
Nov 8 at 2:58
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187770%2fwhen-adding-new-product-programmatically-to-existing-cart-in-magento-2-product%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown