Here is the code for the method I use to add / update of shopping cart items. What I want it to do is:
if new product then add
else if product existing as cart item then update the quantity
It does not currently update properly, any thoughts?
Also, the table for the shopping cart items is:
ShoppingCartID int, PK
ProductID int, PK
Quantity
public bool AddShoppingCartItem( int shoppingCartID, int productID, int quantity )
{
// Make sure parameters are valid.
if ( shoppingCartID == 0 || productID == 0 || quantity == 0 )
{
return false;
}
try
{
// Declare the Data Adapter.
using ( DataAccessAdapter da = new DataAccessAdapter() )
{
// Set the _shoppingCartItemEntity to Dirty =
_shoppingCartItemEntity.IsNew = true;
_shoppingCartItemEntity.IsDirty = true;
// Set the Primary Key Values.
_shoppingCartItemEntity.ShoppingCartId = shoppingCartID;
_shoppingCartItemEntity.ProductId = productID;
// Does the item already exist?
if ( da.FetchEntity( _shoppingCartItemEntity ) )
{
_shoppingCartItemEntity.Quantity += quantity;
}
else
{
_shoppingCartItemEntity.Quantity = quantity;
}
// Save the _shoppingCartItemEntity.
da.SaveEntity( _shoppingCartItemEntity, false );
}
}
catch ( Exception ex )
{
throw new BizException( "SolutionDesigners.Biz.Shopping.CreateShoppingCartItem( int shoppingCartID, int productID, int quantity ) Exception - " + ex.Message );
}
return true;
}
Thanks,
Wade