Adding to cart via flash or other type of post method

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Adding to cart via flash or other type of post method

Post by mazhar » Tue Apr 14, 2009 12:18 pm

You can send multiple product ids via single URL and then load and them to basket similarly as WIKI post is doing for a single product.

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Adding to cart via flash or other type of post method

Post by mazhar » Tue Apr 14, 2009 12:31 pm

For example URL could be like

Code: Select all

http://yourstoreurl/AddToBasket.aspx?ProductIds=1,2,3,4,5,6,7,8
Then in control part you can update Page_Load method like

Code: Select all

 protected void Page_Load(object sender, EventArgs e)
    {
        string[] productIds = Request.QueryString["ProductIds"].Split(',');
        foreach(string productId in productIds)
{
_ProductId = AlwaysConvert.ToInt(productId);
        if (_ProductId > 0)
            AddToCart();
}
    }

Regarding custom fields yes you I think its possible. Give a try and make your URL something like

Code: Select all

http://yourstoreurl/AddToBasket.aspx?ProductIds=1-c1:c2,2,3,4,5,6,7,8
Now in Page_Load you can first split on "," which will return you product id with fields. Then split each string on "-" which will give you one product id one list of custom fields. Finally split each list of custom fields to get each single custom field info

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Tue Apr 14, 2009 3:34 pm

I would love it if you could provide working code when you get this to work please. I also have a flash program I would like to us to add a string of items to a order.

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Wed Apr 15, 2009 10:09 am

I messed around with this just trying to add a single item to the cart via a URL using the WIKI code and couldnt get it to work. Can you provide a working sample of this?

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Adding to cart via flash or other type of post method

Post by mazhar » Wed Apr 22, 2009 7:02 am

Once you are able to get text from URL you can try to add this text to customer input in some quite similar manner as ProductHelper methods are collecting customer input. Check App_Code/ProductHelper.cs class about how to manipulate product inputs.

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Wed Apr 29, 2009 2:20 pm

Mazhar,

Thanks to Joe, I got the wiki code to work and I can add a string of simple items fine. Is there anyway to make it add kits? I have a lot of kits that DO NOT have choices that need to be made, is there anyway to make it add them to the cart correctly?

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Adding to cart via flash or other type of post method

Post by mazhar » Wed May 06, 2009 10:34 am

compunerdy wrote:Mazhar,

Thanks to Joe, I got the wiki code to work and I can add a string of simple items fine. Is there anyway to make it add kits? I have a lot of kits that DO NOT have choices that need to be made, is there anyway to make it add them to the cart correctly?
Give a try and edit the code you got from WIKI, locate following code block

Code: Select all

if (product.HasChoices)
            {
                //CANT ADD DIRECTLY TO BASKET, SEND TO MORE INFO
                Response.Redirect(p
and make it look like

Code: Select all

if (product.HasChoices)
            {
                if (product.KitStatus == KitStatus.Master)
                {
                    bool addToCart = true;
                    KitComponentCollection kitComponents = KitComponentDataSource.LoadForProduct(_ProductId);
                    foreach (KitComponent kitComponent in kitComponents)
                    {
                        if (kitComponent.InputType == KitInputType.CheckBox || kitComponent.InputType == KitInputType.DropDown || kitComponent.InputType == KitInputType.RadioButton)
                        {
                            addToCart = false;
                            break;
                        }
                    }
                    if (!addToCart)
                        //CANT ADD DIRECTLY TO BASKET, SEND TO MORE INFO
                        Response.Redirect(product.NavigateUrl);
                }
                else
                {
                    //CANT ADD DIRECTLY TO BASKET, SEND TO MORE INFO
                    Response.Redirect(product.NavigateUrl);
                }
                
            }

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Fri May 15, 2009 9:24 am

Mazhar

Here is the code I am using. It will now add the parent item of a kit with 0 price but it still wont add the included/hidden sub items.

addtobasket.aspx

Code: Select all

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="AddToBasket.aspx.vb" Inherits="AddToBasket" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

<% Response.Redirect("basket.aspx") %>
addtobasket.aspx.vb

Code: Select all

Partial Class AddToBasket
    Inherits System.Web.UI.Page

    Dim _ProductId As Integer


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim _ProductIds As String() = Request.QueryString("ProductIds").Split(",")
        For Each _Param As String In _ProductIds
            _ProductId = AlwaysConvert.ToInt(_Param)
            AddToCart()
        Next

    End Sub

    Protected Sub AddToCart()
        'GET THE PRODUCT ID FROM THE URL
        Dim product As Product = ProductDataSource.Load(_ProductId)

        If product IsNot Nothing Then
            Dim lastShoppingUrl As String = NavigationHelper.GetLastShoppingUrl()
            If product.HasChoices Then
                If product.KitStatus = KitStatus.Master Then
                    Dim _addtocart As Boolean = True
                    Dim _kitcomponents As KitComponentCollection = KitComponentDataSource.LoadForProduct(product.ProductId)
                    For Each _KitComponent As KitComponent In _kitcomponents
                        If _KitComponent.InputType = KitInputType.CheckBox Or _KitComponent.InputType = KitInputType.DropDown Or _KitComponent.InputType = KitInputType.RadioButton Then
                            _addtocart = False
                            Exit For
                        End If
                    Next

                    If Not _addtocart Then
                        'CANT ADD DIRECTLY TO BASKET, SKIP IT
                        Return
                    End If
                Else
                    'CANT ADD DIRECTLY TO BASKET, SKIP IT
                    Return
                End If
            End If

            Dim basketItem As BasketItem = BasketItemDataSource.CreateForProduct(_ProductId, 1)
            If basketItem IsNot Nothing Then
                'ADD ITEM TO BASKET
                Dim basket As Basket = Token.Instance.User.Basket
                basket.Items.Add(basketItem)
                basket.Save()

                ' IF BASKET HAVE SOME VALIDATION PROBLEMS MOVE TO BASKET PAGE
                Dim basketMessages As List(Of String)
                If Not basket.Validate(basketMessages) Then
                    Session.Add("BasketMessage", basketMessages)
                    Response.Redirect(NavigationHelper.GetBasketUrl())
                End If
            End If
        End If
    End Sub

End Class

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Adding to cart via flash or other type of post method

Post by mazhar » Tue Jun 09, 2009 5:26 am

Try following updated control file. Now the code will add Included-Hidden/Included-Shown kit components as well to basket.

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Tue Jun 09, 2009 11:14 am

Having trouble getting the new code to work at all. Maybe I am just not using it correctly as the code Joe got working for me (posted above) is in VB and yours is C#. If I try to access your new file directly it gives me a server error. Can you provide a working file set that I can just upload and use?

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Adding to cart via flash or other type of post method

Post by mazhar » Wed Jun 10, 2009 7:10 am

Could you post server error that occurs if you directly use the file.

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Wed Jun 10, 2009 8:31 am

Can you mod this page to use the new user control?

addtobasket.aspx

Code: Select all

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="AddToBasket.aspx.vb" Inherits="AddToBasket" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

<% Response.Redirect("basket.aspx") %>

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Adding to cart via flash or other type of post method

Post by mazhar » Wed Jun 10, 2009 8:38 am

Instead of using complete file just update the required part. Locate following code part

Code: Select all

 if (product.HasChoices)
            {
                //CANT ADD DIRECTLY TO BASKET, SEND TO MORE INFO
                Response.Redirect(product.NavigateUrl);
            }
with below

Code: Select all

if (product.HasChoices)
            {
                if (product.KitStatus == KitStatus.Master)
                {
                    List<int> selectedKitProducts = new List<int>();
                    //COLLECT ANY KIT VALUES
                    foreach (ProductKitComponent pkc in product.ProductKitComponents)
                    {
                        // FIND THE CONTROL
                        KitComponent component = pkc.KitComponent;
                        if (component.InputType == KitInputType.IncludedHidden || component.InputType == KitInputType.IncludedShown)
                        {
                            foreach (KitProduct choice in component.KitProducts)
                            {
                                selectedKitProducts.Add(choice.KitProductId);
                            }
                        }
                    }

                    BasketItem kitBasketItem = BasketItemDataSource.CreateForProduct(_ProductId, 1, string.Empty, selectedKitProducts);

                    Token.Instance.User.Basket.Items.Add(kitBasketItem);
                    Token.Instance.User.Basket.Save();
                    //IF THERE IS NO REGISTERED BASKET CONTROL, WE MUST GO TO BASKET PAGE
                    if (!PageHelper.HasBasketControl(this.Page)) Response.Redirect(NavigationHelper.GetBasketUrl());
                }


                //CANT ADD DIRECTLY TO BASKET, SEND TO MORE INFO
                Response.Redirect(product.NavigateUrl);
            }

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Thu Jun 11, 2009 9:05 am

I updated my code above with your new code like this..

Code: Select all

Partial Class AddToBasket
    Inherits System.Web.UI.Page

    Dim _ProductId As Integer


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim _ProductIds As String() = Request.QueryString("ProductIds").Split(",")
        For Each _Param As String In _ProductIds
            _ProductId = AlwaysConvert.ToInt(_Param)
            AddToCart()
        Next

    End Sub

    Protected Sub AddToCart()
        'GET THE PRODUCT ID FROM THE URL
        Dim product As Product = ProductDataSource.Load(_ProductId)

        If product IsNot Nothing Then
            Dim lastShoppingUrl As String = NavigationHelper.GetLastShoppingUrl()



If product.HasChoices Then
    If product.KitStatus = KitStatus.Master Then
        Dim selectedKitProducts As New List(Of Integer)()
        'COLLECT ANY KIT VALUES
        For Each pkc As ProductKitComponent In product.ProductKitComponents
            ' FIND THE CONTROL
            Dim component As KitComponent = pkc.KitComponent
            If component.InputType = KitInputType.IncludedHidden OrElse component.InputType = KitInputType.IncludedShown Then
                For Each choice As KitProduct In component.KitProducts
                    selectedKitProducts.Add(choice.KitProductId)
                Next
            End If
        Next
        
        Dim kitBasketItem As BasketItem = BasketItemDataSource.CreateForProduct(_ProductId, 1, String.Empty, selectedKitProducts)
        
        Token.Instance.User.Basket.Items.Add(kitBasketItem)
        Token.Instance.User.Basket.Save()
        'IF THERE IS NO REGISTERED BASKET CONTROL, WE MUST GO TO BASKET PAGE
        If Not PageHelper.HasBasketControl(Me.Page) Then
            Response.Redirect(NavigationHelper.GetBasketUrl())
        End If
    End If
    
    
    'CANT ADD DIRECTLY TO BASKET, SEND TO MORE INFO
    Response.Redirect(product.NavigateUrl)
End If



            Dim basketItem As BasketItem = BasketItemDataSource.CreateForProduct(_ProductId, 1)
            If basketItem IsNot Nothing Then
                'ADD ITEM TO BASKET
                Dim basket As Basket = Token.Instance.User.Basket
                basket.Items.Add(basketItem)
                basket.Save()

                ' IF BASKET HAVE SOME VALIDATION PROBLEMS MOVE TO BASKET PAGE
                Dim basketMessages As List(Of String)
                If Not basket.Validate(basketMessages) Then
                    Session.Add("BasketMessage", basketMessages)
                    Response.Redirect(NavigationHelper.GetBasketUrl())
                End If
            End If
        End If
    End Sub

End Class

Now I get this error.

Code: Select all

Compiler Error Message: BC30311: Value of type 'System.Collections.Generic.List(Of Integer)' cannot be converted to 'String'.

Source Error:

 

Line 38:         Next
Line 39:         
Line 40:         Dim kitBasketItem As BasketItem = BasketItemDataSource.CreateForProduct(_ProductId, 1, String.Empty, selectedKitProducts)
Line 41:         
Line 42:         Token.Instance.User.Basket.Items.Add(kitBasketItem)
 

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Adding to cart via flash or other type of post method

Post by mazhar » Thu Jun 11, 2009 9:43 am

That's strange I just created a VB page put all your code in it and its working great. Check attached file. What is your application version I have tested code on 7.0.2

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Thu Jun 11, 2009 9:58 am

I just installed those files and I am still getting the same error as seen here..

http://www.thecustomsabershop.com/addto ... uctIds=183

Build 7.0.3

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Adding to cart via flash or other type of post method

Post by mazhar » Thu Jun 11, 2009 10:06 am

Yep just found that those files are not working on 7.0.3.

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Adding to cart via flash or other type of post method

Post by mazhar » Thu Jun 11, 2009 10:40 am

Locate following line of code in your page

Code: Select all

Dim kitBasketItem As BasketItem = BasketItemDataSource.CreateForProduct(_ProductId, 1, String.Empty, selectedKitProducts)
and replace it with following

Code: Select all

Dim kitBasketItem As BasketItem = BasketItemDataSource.CreateForProduct(_ProductId, 1, String.Empty, AlwaysConvert.ToList(",", selectedKitProducts))
save file and then try to add some kit products via URL.

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Thu Jun 11, 2009 10:49 am

Looks great so far..thanks Mazhar!!!

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Adding to cart via flash or other type of post method

Post by mazhar » Thu Jun 11, 2009 10:51 am

Sounds good

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Thu Jun 11, 2009 10:59 am

I spoke too soon..

It works fine for a single item like this..

http://www.thecustomsabershop.com/addto ... uctIds=183

but not for a string like this

http://www.thecustomsabershop.com/addto ... ds=183,184

It only adds the first item.

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Fri Jun 12, 2009 12:20 pm

I did some more testing. When adding items via the URL string...

If you have a kit item then all items (kit or regular) in the string after it will be ignored.

Example http://www.thecustomsabershop.com/addto ... 183,184,73

If you have a non kit item it is added to cart but the redirect to cart doesnt work.

Example http://www.thecustomsabershop.com/addto ... ductIds=73

You can have multiple non kit items and then a single kit item and it all works fine.

Example http://www.thecustomsabershop.com/addto ... 73,107,183

So.. kit items are not looping and regular items are stopping the code??? Something like that

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Adding to cart via flash or other type of post method

Post by mazhar » Sat Jun 13, 2009 1:03 am

Tim thanks for detailed information about reproduction cases. I have seen the code and there were some things needs to be updated. Here is updated page

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Sun Jun 14, 2009 8:00 pm

I had to make a change but now it works great. I had to move a else if as it wasnt processing normal items. Here is the final code that works perfectly (as far as I can tell so far)

AddToBasket.aspx.vb

Code: Select all

Partial Class AddToBasket
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddToCart()
    End Sub

    Protected Sub AddToCart()

        Dim productParams As String
        productParams = Request.QueryString("ProductIds")

        If (String.IsNullOrEmpty(productParams)) Then
            Response.Redirect(Token.Instance.Store.StoreUrl)
        End If

        Dim productIds As String() = productParams.Split(",")

        For Each id As String In productIds
            Dim productId As Integer = AlwaysConvert.ToInt(id)
            ''GET THE PRODUCT ID FROM THE URL
            Dim product As Product = ProductDataSource.Load(productId)

            If product IsNot Nothing Then
                If product.HasChoices Then
                    If product.KitStatus = KitStatus.Master Then
                        Dim selectedKitProducts As New List(Of Integer)
                        ''COLLECT ANY KIT VALUES
                        For Each pkc As ProductKitComponent In product.ProductKitComponents
                            '' FIND THE CONTROL
                            Dim component As KitComponent = pkc.KitComponent
                            If component.InputType = KitInputType.IncludedHidden OrElse component.InputType = KitInputType.IncludedShown Then
                                For Each choice As KitProduct In component.KitProducts
                                    selectedKitProducts.Add(choice.KitProductId)
                                Next
                            End If
                        Next

                        Dim kitBasketItem As BasketItem = BasketItemDataSource.CreateForProduct(productId, 1, String.Empty, AlwaysConvert.ToList(",", selectedKitProducts.ToArray()))
                        Token.Instance.User.Basket.Items.Add(kitBasketItem)
                        Token.Instance.User.Basket.Save()
                    Else
                        Dim basketItem As BasketItem = BasketItemDataSource.CreateForProduct(productId, 1)
                        Token.Instance.User.Basket.Items.Add(basketItem)
                        Token.Instance.User.Basket.Save()
                    End If
	    	Else
                        Dim basketItem As BasketItem = BasketItemDataSource.CreateForProduct(productId, 1)
                        Token.Instance.User.Basket.Items.Add(basketItem)
                        Token.Instance.User.Basket.Save()
                End If
            End If

        Next
        Response.Redirect(NavigationHelper.GetBasketUrl())
    End Sub

End Class
AddToBasket.aspx

Code: Select all

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="AddToBasket.aspx.vb" Inherits="AddToBasket" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Anyone wanting to see this all in action can check out my flash program here
http://www.thecustomsabershop.com/mhsbuilder/
Just add some items to the screen and then click on "parts list" at the top and then "add to cart"

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Adding to cart via flash or other type of post method

Post by compunerdy » Tue Jul 05, 2011 11:34 pm

Any idea as to why after upgrading to 7.0.7 and .net 4.0 the above code no longer works and gives the following error?

C:\Sites\thecustomsabershop\AddToBasket.aspx.vb(47): error BC32206: The project currently contains references to more than one version of CommerceBuilder.Configuration, a direct reference to version 7.7.14421.0 and an indirect reference (through 'CommerceBuilder.Common.Token') to version 7.7.14567.0. Change the direct reference to use version 7.7.14567.0 (or higher) of CommerceBuilder.Configuration.

Post Reply