Important Notice from AspDotNetStorefront
It is with dismay that we report that we have been forced, through the action of hackers, to shut off write-access to this forum. We are keen to leave the wealth of material available to you for research. We have opened a new forum from which our community of users can seek help, support and advice from us and from each other. To post a new question to our community, please visit: http://forums.vortx.com
Results 1 to 32 of 32

Thread: Programmatically Adding To the Cart

  1. #1
    Rob is offline Senior Member
    Join Date
    Aug 2004
    Posts
    3,037

    Default Programmatically Adding To the Cart

    We get a lot of questions about how to add to the cart programmatically, via a link, or button, etc. The syntax is very simple. Just link to:

    addtocart.aspx?productid=M&variantid=N

    That is the absolute minimum syntax

    The next most common syntax is:

    addtocart.aspx?productid=M&variantid=N&quantity=Q

    the full syntax could be:

    addtocart.aspx?ProductID=M&VariantID=N&Quantity=Q& ReturnURL=whatever.aspx&price=x.xx&Color=red&Size= L&ShippingAddressID=R

    price is for forcing price in (note this may not be honored for all product types, e.g. kits).

    if ProductID is not specified, it will be determined from the VariantID

    If VariantID is 0 or not specified, nothing will get added

    Size and Color values MUST be in master web.config locale value form (for multi-lingual sites, and properly URL encoded)

    ReturnURL must be properly URL encoded

    Quantity defaults to 1 if not specified
    Last edited by Rob; 02-24-2007 at 04:50 PM.

  2. #2
    fsantos is offline Senior Member
    Join Date
    Feb 2007
    Posts
    244

    Default

    If price can be forced, isn't this a security problem?

    Someone can eventually use this URL and add the product to the cart at a cheaper price...

    Even though I tried it on our website and it didn't work I would like to know more about this "forced price" thing.

    Fsantos

  3. #3
    ntm is offline Junior Member
    Join Date
    Nov 2006
    Posts
    5

    Default

    Looks like the price in the querystring only works if the product is flagged to allow the customer to enter the price.

  4. #4
    Rob is offline Senior Member
    Join Date
    Aug 2004
    Posts
    3,037

    Default

    Yes, the product must be setup to even allow a price to be "forced" in

  5. #5
    jbrinkman's Avatar
    jbrinkman is offline Member
    Join Date
    Apr 2006
    Posts
    33

    Default

    One problem with the current code:

    If I enter a ReturnUrl, I expect that to be the URL used even if I stay on the ShoppingCart page. As it stands now if you launched the shopping cart from another site and want to have the user stay in the store, there is no way to do so.
    Joe Brinkman
    ASP.Net MVP
    DotNetNuke CoreTeam (http://www.dotnetnuke.com)

  6. #6
    Rob is offline Senior Member
    Join Date
    Aug 2004
    Posts
    3,037

    Default

    could add 1 line of code that checks for begins with http and then do a hard full redirect afterwards

  7. #7
    jbrinkman's Avatar
    jbrinkman is offline Member
    Join Date
    Apr 2006
    Posts
    33

    Default

    Here is the relevant section of code at the end of the Page_Load event:

    Code:
    if (AppLogic.AppConfig("AddToCartAction").ToUpperInvariant() == "STAY" && ReturnURL.Length != 0)
    {
    Response.Redirect(ReturnURL);
    }
    else
    {
    if (ReturnURL.Length == 0)
    {
       ReturnURL = String.Empty;
      if (Request.UrlReferrer != null)
       {
         ReturnURL = Request.UrlReferrer.AbsoluteUri; // could be null
       }
      if (ReturnURL == null)
      {
         ReturnURL = String.Empty;
      }
    }
    if (CartType == CartTypeEnum.WishCart)
    {
    Response.Redirect("wishlist.aspx?ReturnUrl=" + Security.UrlEncode(ReturnURL));
    }
    if (CartType == CartTypeEnum.GiftRegistryCart)
    {
    Response.Redirect("giftregistry.aspx?ReturnUrl=" + Security.UrlEncode(ReturnURL));
    }
    Response.Redirect("ShoppingCart.aspx?add=true&ReturnUrl=" + Security.UrlEncode(ReturnURL));
    }
    In the line marked in Orange, there are two conditions that could cause the check to fail:
    1. AddToCartAction is not set to 'STAY' OR
    2. No ReturnUrl was specified in the querystring

    In the "else" branch, the original code performed the same steps regardless of the reason for falling through to this branch. I have added the ReturnURL.Length==0 (in red) check so that it mimics condition #2. Essentially, if the user supplied a return URL then we don't want to reset it in the "else" branch.
    Joe Brinkman
    ASP.Net MVP
    DotNetNuke CoreTeam (http://www.dotnetnuke.com)

  8. #8
    Rob is offline Senior Member
    Join Date
    Aug 2004
    Posts
    3,037

    Default

    I'll get it updated to reflect this logic. Sounds good.

  9. #9
    fsantos is offline Senior Member
    Join Date
    Feb 2007
    Posts
    244

    Default

    This is a great contributing community and also a great company supporting our requests. Makes me VERY happy that I am paying for yearly support because I see the return on that investment.

    Thank you for all of you contributing with improvements, and thank you ADNSF team for listening to us and doing it.

    fsantos

  10. #10
    Alkaline is offline Senior Member
    Join Date
    May 2006
    Posts
    459

    Default

    please sticky this! I hate it when these valuable posts get deleted during monthly maintenance!

  11. #11
    Rob is offline Senior Member
    Join Date
    Aug 2004
    Posts
    3,037

    Default

    it is already sticky...and has been for months.

  12. #12
    toofast is offline Senior Member
    Join Date
    Dec 2005
    Location
    Cherry Hill, NJ, USA
    Posts
    239

    Default

    Is there a way to use this code dynamically? I'm guessing the code would look something like this:
    C#/VB.NET Code:
    <xsl:value-of select="concat('<a href=/addtocart.aspx?productid=', ProductID, '&variantid=', VariantName, '/>')" />
    <
    img src="/images/addtocartbutton.jpg"></a
    I wish there was an AddtoCart XSL extension fucntion so we could do this:
    C#/VB.NET Code:
    <a href="{aspdnsf:AddtoCartLink(ProductID)}">add to cart</a

  13. #13
    toofast is offline Senior Member
    Join Date
    Dec 2005
    Location
    Cherry Hill, NJ, USA
    Posts
    239

    Default

    Got it!
    Code:
    <a href="/addtocart.aspx?productid={aspdnsf:Decode(ProductID)}&amp;variantid={aspdnsf:Decode(VariantID)}">
    <img src="/images/addtocartbutton.jpg" /></a>
    Last edited by Rob; 12-06-2007 at 09:42 AM.

  14. #14
    Rob is offline Senior Member
    Join Date
    Aug 2004
    Posts
    3,037

    Default

    why are you having to decode integers?

  15. #15
    itguy79 is offline Junior Member
    Join Date
    Jul 2007
    Posts
    8

    Default

    Has anyone coded like this for the kits at all? The logic is obviously different from a regular product but was wondering if its been done, or can be done.
    Thanks Dan

  16. #16
    Rob is offline Senior Member
    Join Date
    Aug 2004
    Posts
    3,037

    Default

    RE: "Kits"...no, that will not work, Kits are more complicated.
    AspDotNetStorefront
    Shopping Cart

  17. #17
    sephi2483 is offline Member
    Join Date
    Feb 2008
    Location
    Davao City, Philippines
    Posts
    32

    Default

    is there a way in shopping cart to combine two items into one

    e.g.

    product 1
    -----------
    product 2
    -----------
    product 3
    -----------
    product 4
    -----------

    into:

    product 1
    product 2
    -----------
    product 3
    product 4
    -----------
    <O HAI!11!1111 />

    Everything in between

    Moushie's Blog
    http://www.moushigo.com/

    My Online English-To-LOLspeak Translator!
    http://www.moushigo.com/index.php/the-loler-project/

    <KBYTHX!!11!!11 />

  18. #18
    Rob is offline Senior Member
    Join Date
    Aug 2004
    Posts
    3,037

    Default

    what do you mean "into one"?
    AspDotNetStorefront
    Shopping Cart

  19. #19
    sephi2483 is offline Member
    Join Date
    Feb 2008
    Location
    Davao City, Philippines
    Posts
    32

    Default

    Quote Originally Posted by sephi2483 View Post
    is there a way in shopping cart to combine two items into one

    e.g.

    product 1
    -----------
    product 2
    -----------
    product 3
    -----------
    product 4
    -----------

    into:

    product 1
    product 2
    -----------
    product 3
    product 4
    -----------

    sorry, what I meant was to group them based on a common field, any common field

    (Shopping Cart page)
    product 1
    product 2
    -----------
    product 3
    product 4
    -----------

    So that each group of products will be assigned with a shipping address, and the other perhaps another shipping address
    <O HAI!11!1111 />

    Everything in between

    Moushie's Blog
    http://www.moushigo.com/

    My Online English-To-LOLspeak Translator!
    http://www.moushigo.com/index.php/the-loler-project/

    <KBYTHX!!11!!11 />

  20. #20
    tcezeaux is offline Junior Member
    Join Date
    Apr 2008
    Posts
    10

    Default Only ProductID=1 works with /addtocart.aspx

    Weird error. I followed the information in this thread, and it worked - but not fully.

    If I have a URL of /addtocart.aspx?ProductID=1&VariantID=1&Quantity=1 it works fine. Different quantities, no problem.

    However, if I change the ProductID to anything except 1, it goes to the ShoppingCart.aspx page, but no product is added.

    The product can be manually added to the cart (through the normal shopping process), but to do it programmatically just doesn't work.

    Any ideas would be helpful.

    Thanks

  21. #21
    sephi2483 is offline Member
    Join Date
    Feb 2008
    Location
    Davao City, Philippines
    Posts
    32

    Default

    Quote Originally Posted by tcezeaux View Post
    Weird error. I followed the information in this thread, and it worked - but not fully.

    If I have a URL of /addtocart.aspx?ProductID=1&VariantID=1&Quantity=1 it works fine. Different quantities, no problem.

    However, if I change the ProductID to anything except 1, it goes to the ShoppingCart.aspx page, but no product is added.

    The product can be manually added to the cart (through the normal shopping process), but to do it programmatically just doesn't work.

    Any ideas would be helpful.

    Thanks
    probably VariantID 1 is not joined to the new ProductID
    <O HAI!11!1111 />

    Everything in between

    Moushie's Blog
    http://www.moushigo.com/

    My Online English-To-LOLspeak Translator!
    http://www.moushigo.com/index.php/the-loler-project/

    <KBYTHX!!11!!11 />

  22. #22
    tcezeaux is offline Junior Member
    Join Date
    Apr 2008
    Posts
    10

    Default Variant required?

    Partial success.

    If I explicitly put the VariantID onto the QueryString, then it works.

    However, if I don' t have the variantid, then the Default Variant is not used.

    Is there a way to obtain the Default Variant ID based on the Product ID?

  23. #23
    Rob is offline Senior Member
    Join Date
    Aug 2004
    Posts
    3,037

    Default

    Well, odd, it looks like we won't create the default variantid if it's passed in as 0 ( or missing). I think we should , I'll report over to dev.

    For now, just pass in the correct VariantID that you want added. thx.
    Last edited by Rob; 04-05-2008 at 10:16 PM.
    AspDotNetStorefront
    Shopping Cart

  24. #24
    zalienjoe is offline Member
    Join Date
    Dec 2007
    Posts
    59

    Default

    I'm trying to use this method of adding products to the cart from a list view. I get the following error...

    The page cannot be displayed
    You have attempted to execute a CGI, ISAPI, or other executable program from a directory that does not allow programs to be executed.

    Please try the following:

    * Contact the Web site administrator if you believe this directory should allow execute access.

    HTTP Error 403.1 - Forbidden: Execute access is denied.
    Internet Information Services (IIS)


    Is this an SSL issue or a server setting?


    This is my code. It's pulling the right data and creating the correct string.
    HTML Code:
    <a href="/addtocart.aspx?productid={aspdnsf:GetMLValue(ProductID)}&amp;variantid={aspdnsf:GetMLValue(VariantID)}">
    addToCart</a>

  25. #25
    zalienjoe is offline Member
    Join Date
    Dec 2007
    Posts
    59

    Default

    Also, can this be done with the Wish List as well?

  26. #26
    seasonalsin is offline Senior Member
    Join Date
    Nov 2006
    Posts
    123

    Default

    What would the syntax be to add multiple parts to the cart. Say 2 of widget a and 3 of widget b? If I a getting this correctly I would only have to return the variant ID as well. Correct?
    Shawn

    http://www.FirstChoiceMarine.com

    Currently 7.1 moving to MS9.3

  27. #27
    tlister is offline Junior Member
    Join Date
    Dec 2008
    Posts
    1

    Default Changing colour of link

    I have used this code to make an embedded link to add products to the shop without leaving the page. The code used uses php to wrap the values pulled from the database:

    Code:
    	$button = "<a href=\"javascript:void(0);\" class=\"univisted\" onclick=\"this.className='visited';window.location.href='http://www.canonbury.com/addtocart.aspx?productid=";
       		$button .= $productid;
       		$button .= "&variantid=";
       		$button .= $varid;
       		$button .= "&quantity=";
    		$button .= $quantity;
    		$button .= "&color=";
       		$button .= "";
       		$button .= $colour;
       		$button .= "&ReturnURL=http://www.canonbury.com/mytest/query2.php&size=";
       		$button .= $size;
       		$button .= "'\">";
       		$button .= "buy now";
       		$button .= "</a>";
    The problem is that it once it has added doesn't change colour like a normal link as doesn't 'go anywhere'. I'd be grateful if anyone had any ideas on how to change the colour of the link to show that a product has been added to the cart.

  28. #28
    Emissary is offline Member
    Join Date
    Sep 2008
    Location
    Florida
    Posts
    60

    Talking Gift or Wishlist too?

    Does anyone know if this same thing could be applied to the Gift or Wishlist?

  29. #29
    lambego is offline Junior Member
    Join Date
    Nov 2009
    Posts
    1

    Post adding more than one product to cart

    i am trying to add more than one product to cart from the product details page. Is it possible?

    My scenario is like i have one product (one variant). It has 2 colors (red, green) and 3 sizes (small, medium, large). Now i want to add 2 products for red (small), and 1 product for red (large).

    I have the source code so i was planning to modify the form for add to cart. Currently it has one Color, one Size and one Quantity fields and when we post this to addtocart.aspx, it adds the product to cart perfectly.
    Now, if i create more Size, Color and Quantity fields in that form and then post it to addtocart.aspx. What changes do i have to do to addtocart.aspx so that it handles this multi add request.

    Is this the proper way to do this kind of thing? If not then please suggest some workaround.

    thanks

  30. #30
    bstephenson is offline Junior Member
    Join Date
    Nov 2009
    Posts
    4

    Default

    Is there any analogous process for StoreFront/DNN? And question 2, can I redirect to another page (show cart page) after adding to cart, and ensure all products for the "user" whose cart I am manipulating in code, are in that cart?


    Thanks.

  31. #31
    bstephenson is offline Junior Member
    Join Date
    Nov 2009
    Posts
    4

    Default

    I would like to add 1 to N products from a predetermined list (based on user criteria) to a shopping cart in code, and THEN get re-directed to the Shopping Cart page to see the results of filling the cart with the pre-selected items. Is this easy in ML, using the URL add to cart trick, or should it all be done in .Net code using the proper classes?

    Thx
    Brian

  32. #32
    dazzamatazz is offline Junior Member
    Join Date
    May 2007
    Posts
    5

    Default formValidate.js error on add to cart button in IE

    I am having a major problem where I am unable to add to cart in IE. I get an error
    stating there was something wrong with my file formValidate.js line 918 character 4.

    Can you please let me know if I am missing something in my files that is not letting it add the products to my cart.

    This is the function that was being referenced before that I had and error
    function submitonce(theform)
    {
    if (document.all||document.getElementById)
    {
    for (i=0;i<theform.length;i++)
    { var tempobj=theform.elements[i];
    if(tempobj.type.toLowerCase()=="submit" || tempobj.type.toLowerCase()=="reset")
    tempobj.disabled=true;
    }}}

    Any help would be much appreciated.

    Regards,

    Daz