cancel
Showing results for 
Search instead for 
Did you mean: 

Add Item To Current User's Cart Using API

Former Member
0 Kudos

I'm using Web Tools v625. I'm looking to use the API to add a part to the user's cart, but I can't figure out how to do it. I have a quick example below:


// (Account ID, Order Type)
netpoint.api.commerce.NPOrder cart =
  new netpoint.api.commerce.NPOrder("MyAccountID", netpoint.api.CartType.Cart);
netpoint.api.catalog.NPPartPricingMaster priceList =
  new netpoint.api.catalog.NPPartPricingMaster("MyPriceList");
// (Part Number, Quantity, Catalog Code, Category Code, Price List)
cart.AddPart("MyPartNo", 1, "MyCatalogCode", "MyCategoryCode", priceList);
cart.Save();

It seems that this should work, except that I need the user to be logged in. I looked at the data in OrderMaster, and there is a field for both AccountID and UserID, but many of the records do not contain any values in these fields. Instead, there is a consistently unique hash value in the CustKey field, which I'm guessing is how each cart is differentiated on a per-user basis, regardless of login status.

I'm a little confused on the constructor. It seems that no matter which one I use, it requires a UserID or AccountID, and some even require the CustKey, which, according to the API documentation, is the Session ID. How do I load the current user's cart via the constructor? Additionally, how can I obtain the current user's CustKey/SessionID?

View Entire Topic
Former Member
0 Kudos

I have, yes. The full code is included below:


  protected void AddToCartButton_Click(object sender, EventArgs e) {
    NPBasePage bp = (NPBasePage)Page;
    NPOrder o = new NPOrder(bp.UserID, bp.SessionID);
    if (o.Initialized == false)
      o.CreateDefaultOrder(bp.UserID, bp.AccountID, bp.SessionID);
    string PartNo = "MyPartNo";
    string Notes = "MyNotes";
    int Quantity = 1;
    try {
      o.AddPart(PartNo, Quantity, "CL_001", "", bp.PriceList, Notes);
      // Had to return here because Transfer and Redirect both throw exceptions
      return;
      Server.Transfer("~/commerce/cart.aspx");
      Response.Redirect("~/commerce/cart.aspx");
    }
    catch (Exception ex) {
      throw new Exception("Exception thrown while adding '" + PartNo + "' to cart:\r\n" + ex.Message);
    }
  }

The problem is that when a user is not logged in, there is no bp.UserID and bp.AccountID.

Former Member
0 Kudos

Hi Michael,

This should work. Note that the order is cleared if "c_Part" is the only part left. Otherwise the code would interfere with the "clear cart" event.


        const String c_Part = "B10000";

        NPBasePage bp = (NPBasePage)Page;

        if (bp.AccountID != "") //logged in
        {
            //do something
        }
        else //not logged in
        {

            String sSql = "SELECT OrderID FROM OrderMaster WHERE CustKey ='" + bp.SessionID + "'";
            IDataReader idr = DataFunctions.ExecuteReader(CommandType.Text, sSql, bp.ConnectionString);

            if (idr.Read()) //true if this session created a cart
            {

                //get order associated with this session
                NPOrder o = new NPOrder(idr.GetInt32(0));

                //check if part exists
                Boolean bHasPart = false;
                foreach (NPOrderDetail npod in o.OrderDetail)
                {
                    if (npod.Part.PartNo == c_Part)
                    {
                        bHasPart = true;
                        break;
                    }

                }

                //delete the order if c_Part is the only item it contains
                if (bHasPart && o.OrderDetail.Count == 1)
                {
                    o.Delete();
                    Response.Redirect(Request.Url.ToString()); //refresh cart page

                }

                if (bHasPart) //part already exists 
                {
                    //do something
                }
                else if (o.OrderDetail.Count != 0)//part needs to be added (count will be 0 if cart was cleared)
                {
                    if (o.AddPart(c_Part, 1, bp.CatalogCode, "test", bp.PriceList))
                    {
                        //part added

                        o.Save(); //save change to order                            

                        Response.Redirect(Request.Url.ToString()); //refresh cart page
                    }
                    else
                    {
                        //failed to add part
                    }
                }

            }
            else //cart has not been created
            {
                
            }


        }