I am currently using the category grid conlib with basket options. I have a requirement to add an "add to cart" button next to each item within the datalist instead of the one multiple add to cart button at the bottom of the page. Can you please give me some ideas on how I would do this?
I have tried to add multiple buttons that use the "MultipleAddToBasketButton_Click" function, but for some reason, these buttons don't respond.
Thanks very much in advance for your guidance.
Jamie
Single Add to Cart Button on Category Grid
-
- Ensign (ENS)
- Posts: 14
- Joined: Mon Nov 09, 2009 10:26 am
Re: Single Add to Cart Button on Category Grid
Read following topic
viewtopic.php?f=42&t=8978
viewtopic.php?f=42&t=8978
-
- Ensign (ENS)
- Posts: 14
- Joined: Mon Nov 09, 2009 10:26 am
Re: Single Add to Cart Button on Category Grid
Thanks for the tip. I still cant get the new button to fire - the page just sits there, looks like the command arg is not being registered. Here is what I am doing:
1) Added new button inside datalist: <asp:Button ID="AddToCart" runat="server" Text="AddToCart" CommandName="ADDTOCART" CommandArgument='<%#Eval("ProductId")%>' ></asp:Button>
2) Added OnItemCommand="DataCatalogNodeList_OnItemCommand" to the datalist
2) To my code behind added 2 new functions:
protected void DataCatalogNodeList_OnItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "ADDTOCART")
{
int productId = AlwaysConvert.ToInt(e.CommandArgument);
TextBox control = (TextBox)PageHelper.RecursiveFindControl(e.Item, "Quantity");
AddToBasket(productId, AlwaysConvert.ToInt16(control.Text));
}
}
and
private void AddToBasket(int productId, short quantity)
{
if (quantity <= 0)
quantity = 1;
string lastShoppingUrl = NavigationHelper.GetLastShoppingUrl();
Basket basket = Token.Instance.User.Basket;
//A LIST TO HOLD BASKET ITEMS THAT NEED A LICENCE AGREEMENT TO BE ACCEPTED
List<BasketItem> agreementItems = new List<BasketItem>();
if (quantity > 0)
{
BasketItem basketItem = GetBasketItem(productId, quantity);
// DETERMINE IF THE LICENSE AGREEMENT MUST BE REQUESTED
BasketItemLicenseAgreementCollection basketItemLicenseAgreements = new BasketItemLicenseAgreementCollection(basketItem, LicenseAgreementMode.OnAddToBasket);
if ((basketItemLicenseAgreements.Count > 0))
{
// THESE AGREEMENTS MUST BE ACCEPTED TO ADD TO BASKET
agreementItems.Add(basketItem);
}
else
{
basket.Items.Add(basketItem);
}
}
if (basket.Items.IsDirty)
{
basket.Save();
basket.Package();
basket.Combine();
}
if (agreementItems.Count > 0)
{
// THESE AGREEMENTS MUST BE ACCEPTED TO ADD TO BASKET
string guidKey = Guid.NewGuid().ToString("N");
Cache.Add(guidKey, agreementItems, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0), System.Web.Caching.CacheItemPriority.NotRemovable, null);
string acceptUrl = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("~/Basket.aspx"));
string declineUrl = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(lastShoppingUrl));
Response.Redirect("~/BuyWithAgreement.aspx?Items=" + guidKey + "&AcceptUrl=" + acceptUrl + "&DeclineUrl=" + declineUrl);
}
else
{
//REDIRECT TO BASKET PAGE
//Response.Redirect("~/Basket.aspx");
}
}
However the shopping bar on the left does not update...
Any suggestions appreciated!
Thansk,
Jamie
1) Added new button inside datalist: <asp:Button ID="AddToCart" runat="server" Text="AddToCart" CommandName="ADDTOCART" CommandArgument='<%#Eval("ProductId")%>' ></asp:Button>
2) Added OnItemCommand="DataCatalogNodeList_OnItemCommand" to the datalist
2) To my code behind added 2 new functions:
protected void DataCatalogNodeList_OnItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "ADDTOCART")
{
int productId = AlwaysConvert.ToInt(e.CommandArgument);
TextBox control = (TextBox)PageHelper.RecursiveFindControl(e.Item, "Quantity");
AddToBasket(productId, AlwaysConvert.ToInt16(control.Text));
}
}
and
private void AddToBasket(int productId, short quantity)
{
if (quantity <= 0)
quantity = 1;
string lastShoppingUrl = NavigationHelper.GetLastShoppingUrl();
Basket basket = Token.Instance.User.Basket;
//A LIST TO HOLD BASKET ITEMS THAT NEED A LICENCE AGREEMENT TO BE ACCEPTED
List<BasketItem> agreementItems = new List<BasketItem>();
if (quantity > 0)
{
BasketItem basketItem = GetBasketItem(productId, quantity);
// DETERMINE IF THE LICENSE AGREEMENT MUST BE REQUESTED
BasketItemLicenseAgreementCollection basketItemLicenseAgreements = new BasketItemLicenseAgreementCollection(basketItem, LicenseAgreementMode.OnAddToBasket);
if ((basketItemLicenseAgreements.Count > 0))
{
// THESE AGREEMENTS MUST BE ACCEPTED TO ADD TO BASKET
agreementItems.Add(basketItem);
}
else
{
basket.Items.Add(basketItem);
}
}
if (basket.Items.IsDirty)
{
basket.Save();
basket.Package();
basket.Combine();
}
if (agreementItems.Count > 0)
{
// THESE AGREEMENTS MUST BE ACCEPTED TO ADD TO BASKET
string guidKey = Guid.NewGuid().ToString("N");
Cache.Add(guidKey, agreementItems, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0), System.Web.Caching.CacheItemPriority.NotRemovable, null);
string acceptUrl = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("~/Basket.aspx"));
string declineUrl = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(lastShoppingUrl));
Response.Redirect("~/BuyWithAgreement.aspx?Items=" + guidKey + "&AcceptUrl=" + acceptUrl + "&DeclineUrl=" + declineUrl);
}
else
{
//REDIRECT TO BASKET PAGE
//Response.Redirect("~/Basket.aspx");
}
}
However the shopping bar on the left does not update...
Any suggestions appreciated!
Thansk,
Jamie
-
- Ensign (ENS)
- Posts: 14
- Joined: Mon Nov 09, 2009 10:26 am
Re: Single Add to Cart Button on Category Grid
I am getting into a bad habit of posting and then finding the answer on my own. For anyone looking to add individual add to cart buttons on the each item inside the category grid page instead of a global add multiple to cart, follow the steps in my last post, then, make sure you change EnableViewState="false" in the datalist. Not sure if that is going to mess up paging or anything (likely) but that is how I was able to get it to work.
Suggestions other than the above appreciated.
Cheers,
Jamie
Suggestions other than the above appreciated.
Cheers,
Jamie