on 03-31-2009 5:12 PM
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?
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
{
}
}
Dim bp As NPBasePage = CType(Page, NPBasePage)
Dim o As NPOrder = New NPOrder(bp.UserID, bp.SessionID)
if o.Initialized = false then
o.CreateDefaultOrder(bp.UserID, bp.AccountID, bp.SessionID)
end if
o.AddPart(partnumbertext, qtyint, bp.catalog.catalogcode, "" , bp.PriceList)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
OK, we're halfway there. This function works when the user is logged in - what happens when they're not? It throws an exception ("Object reference not set to an instance of an object.") when I try to add the part to the cart.
I have also one further question, semi-unrelated:
protected void AddToCartButton_Click(object sender, EventArgs e) {
// ...
try {
o.AddPart(PartNo, Quantity, "MyCategoryCode", "", bp.PriceList);
// Both of these will throw exceptions:
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);
}
}
Why can't I redirect after adding the item to the cart?
If you look in the plugins folder you will see user controls that have source code visible. I used this to get started with writing my own code. Specifically if you look at viewcartdetails.ascx you will see the contructor for an order is
NPBasePage bp = (NPBasePage)Page;
NPOrder o = new NPOrder(bp.UserID, bp.SessionID);
The hash value you see is the session id.
Once you have the order object you can use the addpart method.
You also get the pricelist from the basepage instead of creating it yourself.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
97 | |
8 | |
8 | |
5 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.