ECommerceContext.CurrentShoppingCart Issue Using Web Service

Divyanshu Gupta asked on September 29, 2015 16:38

Hi! i am using the following code for creating a cart. Cart is creating properly. But unfortunately i am not able to set or get ECommerceContext.CurrentShoppingCart inside web service.

 
[WebMethod]
public void AddToCart()
{
    ShoppingCartInfo cart = ECommerceContext.CurrentShoppingCart;
    // Assign current shopping cart to current user            
    cart = ShoppingCartInfoProvider.CreateShoppingCartInfo(SiteContext.CurrentSiteID);
    if (cart.ShoppingCartID == 0)
    {
        cart.User = GetUserInfo();
        ShoppingCartInfoProvider.SetShoppingCartInfo(cart);
        ECommerceContext.CurrentShoppingCart = cart;
    }
    // Add item to shopping cart
    ......
}
 
[WebMethod]
public ShoppingCartInfo GetShoppingCartInfo()
{
    ShoppingCartInfo oShoppingCartInfo = ECommerceContext.CurrentShoppingCart;
    if(oShoppingCartInfo.ShoppingCartID != 0)
       oShoppingCartInfo = ShoppingCartInfoProvider.GetShoppingCartInfo(oShoppingCartInfo.ShoppingCartGUID);
    return oShoppingCartInfo;
}

Any guidance would be helpful.

Thanks

Recent Answers


Joshua Adams answered on September 30, 2015 21:10

I don't think that you have to set the current shoppingcart object to the shopping cart object. I think it already has the object loaded, by you calling the setshoppingcartinfo method, its updating the current cart, which is the object.

Here is Kentico's api example from documentation:

  /// <summary>
    /// Adds product to shopping cart. Called when the "Add product to shopping cart" button is pressed.
    /// Expects the CreateProduct method to be run first.
    /// </summary>
    private bool AddProductToShoppingCart()
    {
        // Get the data
        var product = SKUInfoProvider.GetSKUs()
                           .WhereStartsWith("SKUName", "MyNewProduct")
                           .WhereNull("SKUOptionCategoryID")
                           .FirstOrDefault();

        if (product != null)
        {
            // Get current shopping cart
            ShoppingCartInfo cart = ECommerceContext.CurrentShoppingCart;

            // Ensure cart in database            
            ShoppingCartInfoProvider.SetShoppingCartInfo(cart);

            // Add item to cart object
            ShoppingCartItemParameters param = new ShoppingCartItemParameters(product.SKUID, 1);
            ShoppingCartItemInfo cartItem = cart.SetShoppingCartItem(param);

            // Save item to database
            ShoppingCartItemInfoProvider.SetShoppingCartItemInfo(cartItem);

            return true;
        }

        return false;
    }
0 votesVote for this answer Mark as a Correct answer

Divyanshu Gupta answered on October 7, 2015 15:35 (last edited on October 7, 2015 15:50)

Yes, I don't need to set the current shopping cart object to the shopping cart object.

ECommerceContext.CurrentShoppingCart = cart;

Cart has created properly but still i am not able to get ECommerceContext.CurrentShoppingCart.CartItems

[WebMethod]
private string getShoppingCart()
{ 
    ShoppingCartInfo oShoppingCartInfo = ECommerceContext.CurrentShoppingCart;
    bool isCurrentShoppingCartEmpty = false;
    if(ShoppingCart.ShoppingCartGUID ==  Guid.Empty)
         isCurrentShoppingCartEmpty = true;        

    if(!isCurrentShoppingCartEmpty)     
    {
        List<clsShoppingCart> lstShoppingCart = new List<clsShoppingCart>();        
        foreach (ShoppingCartItemInfo item in ECommerceContext.CurrentShoppingCart.CartItems)
        {

        }
    }
    ....
}

I am not getting any cart items which i have added in a previous step..
Getting true value inside isCurrentShoppingCartEmpty variable

Any guidance would be helpful.

Thanks

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.