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.










share|improve this question




























    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.










    share|improve this question


























      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 11:40









      SebaGra

      831925




      831925










      asked Nov 7 at 10:37









      HoangTuan

      11




      11
























          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);





          share|improve this answer




























            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






            share|improve this answer





















            • 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













            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            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

























            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);





            share|improve this answer

























              up vote
              0
              down vote



              accepted










              I need to set multishiping = false before add product.



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





              share|improve this answer























                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);





                share|improve this answer












                I need to set multishiping = false before add product.



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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 at 5:08









                HoangTuan

                11




                11
























                    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






                    share|improve this answer





















                    • 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

















                    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






                    share|improve this answer





















                    • 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















                    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






                    share|improve this answer












                    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







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 7 at 17:21









                    Ulf Tietze

                    11




                    11












                    • 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


















                    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




















                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    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





















































                    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







                    這個網誌中的熱門文章

                    Academy of Television Arts & Sciences

                    L'Équipe

                    1995 France bombings