Manually adding products to the shopping cart

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
I have a specific situation where I have to manually add some products in the shopping cart...

In my method I did it like:

ProductVariant productVariant = _productService.GetProductVariantBySku(item.ID);
                    if (productVariant != null)
                    {
                        var addToCartWarnings = _shoppingCartService.AddToCart(_workContext.CurrentCustomer,
                        productVariant, ShoppingCartType.ShoppingCart,
                        string.Empty, decimal.Zero, 1, true);
                    }


and after that I redirect the user to index page.

For some ugly reason I am not able to find out WHY the items don't appear in the cart (but PLEASE note that I've made debugging and I clearly saw that they were added but only in UI this is not shown).

Can you please let me know is there anything I am missing ?
12 years ago
Does your addToCartWarnings contains any errors?
12 years ago
No, it does not contain..checked it with debug...that's why this seemed even more strange to me...
12 years ago
Could you share your entire solution?
12 years ago
Yes of course:

We use ipaper (a magazine where you can buy some items) and when the user press "Shop" or checkout they REDIRECT with POST the user to our nopcommerce. Their POST comes to my www.mysite.com/iPaperCheckout so I added this method:

[HttpPost, ActionName("IPaperCheckout")]
        public ActionResult IPaperCheckout()
        {
            var xd = new XmlDocument();
           // try
           // {
                StreamReader reader = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);
                String sXMLRequest = reader.ReadToEnd();
                xd.LoadXml(sXMLRequest);


                //xd.LoadXml(xmlTestData);

                string sep = "<br />";

                //Response.Write(xd.InnerXml);

                foreach (XmlNode xn in xd.SelectNodes("//item"))
                {
                    var item = new Item();
                    item.Amount = Convert.ToInt32(xn.SelectSingleNode("amount").InnerText);
                    item.ID = xn.SelectSingleNode("productid").InnerText;

                    Response.Write(string.Format("vareID = {0} - antal = {1}", item.ID, item.Amount));

                    ProductVariant productVariant = _productService.GetProductVariantBySku(item.ID);
                    if (productVariant != null)
                    {
                        var addToCartWarnings = _shoppingCartService.AddToCart(_workContext.CurrentCustomer,
                        productVariant, ShoppingCartType.ShoppingCart,
                        string.Empty, decimal.Zero, 1, true);
                    }

                }
            //}
            //catch (Exception e)
           // {
             //   return RedirectToAction("Index", "Home");
            //}
            return RedirectToAction("Index", "Home");
        }


Maybe it does not help, but for you to better understand my code, their xml is like:

<shop paper=""/Products/Cocacola/"">
                                    <item>
                                        <amount>
                                            2
                                        </amount>
                                        <productid>
                                            <![CDATA[820]]>
                                        </productid>
                                        <price>
                                            99.95
                                        </price>
                                        <name>
                                            <![CDATA[Coca cola]]>
                                        </name>
                                        <description>
                                            <![CDATA[Coca-Cola is a carbonated soft drink]]>
                                        </description>
                                    </item>
                                    <item>
                                        <amount>
                                            1
                                        </amount>
                                        <productid>
                                            <![CDATA[-821]]>
                                        </productid>
                                        <price>
                                            99.95
                                        </price>
                                        <name>
                                            <![CDATA[Coca cola]]>
                                        </name>
                                        <description>
                                            <![CDATA[Coca-Cola is a carbonated soft drink]]>
                                        </description>
                                    </item>
                                </shop>


So I parse their xml succesfully, I found product from my nop db by SKU (actually my client put SKU there so don't be confuse that the xml tag is "productID") and add the corresponding product variant in the shopping cart...pretty simple..
12 years ago
Do you see a new items on the "Current Shopping Carts" page (admin area) - http://admin-demo.nopcommerce.com/Admin/ShoppingCart/CurrentCarts. Are they added to an appropriate customer? Do you see any errors in your log (admin area)?
12 years ago
I can see the items added in Current Shopping carts... The single difference I notice between manually flow and the usual adding-to-cart flow from UI is that in manual flow the Last visited page is not set (because the request comes from outside).

The warnings are 0 and no error in the logs.

Can that "Last visited page" be the issue?

For IP address it puts for both flows: ::1 and last visited page for default flow is http://localhost (because these tests are done from localhost)
12 years ago
I've just tested it with a nopCommerce supported method for adding products:
http://www.youStore.com/cart/addproduct/25 where 25 is your product identifier (it routes to 'AddProductToCart' action method of  'ShoppingCartController'). Everything works fine. Does it work on your machine?
12 years ago
That's a neat feature!  Though I do notice that it's a Product Id, and not a Product Variant Id.  Any way to direct add a variant by id?
12 years ago
New York wrote:
That's a neat feature!  Though I do notice that it's a Product Id, and not a Product Variant Id.  Any way to direct add a variant by id?

This method is used for adding products from category pages (when clicking 'add to cart' button in product boxes).
There's no built-in support for adding product variants, but it can be easily archived the similar way (some minor customization required)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.