ASPX templates
Version 3.x > ASPX templates > can i add to shopping cart using AJAX ? View modes: 
User avatar
Member
Member
mark - 5/5/2009 9:10:21 PM
   
can i add to shopping cart using AJAX ?
im using aspx template method and the CartItemSelector control to generate my ' add to cart' links

<uc1:CartItemSelector id="cartItemSelector" runat="server" SKUID='12' SKUEnabled="true"
AddToCartImageButton="AddToCart.gif" ShowUnitsTextBox="true"
ShowProductOptions="true" AddToCartLinkText="Add to Cart" ShowPriceIncludingTax="true" OnOnAddToShoppingCart />

whenever the 'add to cart' button is pressed the user is redirected to the shopping cart. I wnat them to remain on the product page - the shopping cart insertion should take place in the back ground.

How can this be done ?
Mark

User avatar
Member
Member
mark - 5/5/2009 10:08:24 PM
   
RE:can i add to shopping cart using AJAX ?
an update......

I've ditched the CartItemSelector and am writing my own method using the API.

the code below adds an item into the Cart in memory but not into the database table COM_shoppingCartSKU. How do i persist this data to the database ?


ShoppingCartInfo ShoppingCart = CMS.CMSHelper.CMSContext.CurrentShoppingCart;
int[] aryProductOptions = new int[2] { 14,15 };
ShoppingCart.AddShoppingCartItem(12, 99, aryProductOptions);

I also need to persist the cart data over browser sessions for anonymous users. Is there some clever kentico technique to acheive this or do i need to custom code a cookie based solution ?


User avatar
Member
Member
mark - 5/5/2009 10:47:23 PM
   
RE:can i add to shopping cart using AJAX ?
me again....i've tried adding

ShoppingCartInfoProvider.SetShoppingCartInfo(ShoppingCart);

this does not give an error but also does not write to the database.

The lack of documentation on this is very frustrating - im wasting hours picking through the api guide and trying to reverse engineer the kentico webparts for clues on how this could work.

Kentico - Please take note - your documentation is severely inadequate in this area.

User avatar
Member
Member
mark - 5/5/2009 11:00:16 PM
   
RE:can i add to shopping cart using AJAX ?
eventually i discovred this function below works.

according to the api guide its a deprecated function but its the only method i found that works !

ShoppingCartInfoProvider.SetShoppingCartItems(CMSContext.CurrentShoppingCart.ShoppingCartID, CMSContext.CurrentShoppingCart.CartItems);




User avatar
Member
Member
james@devotion.com.au - 5/6/2009 12:21:35 AM
   
RE:can i add to shopping cart using AJAX ?
but i now get this error when i clear the browser cache

[ShoppingCartItemInfoProvider.SetShoppingCartItemInfo]: No ShoppingCartItemInfo object set.


PLEASE HELP !!!!

User avatar
Member
Member
mark - 5/6/2009 5:57:46 PM
   
RE:can i add to shopping cart using AJAX ?
for anyone else that hits the same wall - heres the reply i received from kentico support

Thank you for your message. Could you please try to use sample code bellow (you will need to modify it to use SkuID and quantity from selector):

// Get current shopping cart

ShoppingCartInfo cart = CMSContext.CurrentShoppingCart;

if (cart == null)

{

// If current shopping cart hasn't been created yet -> create new empty one

cart = ShoppingCartInfoProvider.CreateShoppingCartInfo(CMSContext.CurrentSite.SiteID);



// Associate new shopping cart with the current session

CMSContext.CurrentShoppingCart = cart;

}



int skuId = 10;

int quantity = 1;



// Ensure shopping cart is saved to DB

if (cart.ShoppingCartID == 0)

{

ShoppingCartInfoProvider.SetShoppingCartInfo(cart);

}



// Add product to shopping cart - you can call this method to add multiple products as you need

cart.AddSKUUnitsToShoppingCart(skuId, quantity);



// Save shopping cat products

ShoppingCartInfoProvider.SetShoppingCartItems(cart.ShoppingCartID, cart.CartItems);