Difference between revisions of "Adding products to the cart from external entity"

From AbleCommerce Wiki
Jump to: navigation, search
m
Line 48: Line 48:
 
                     Response.Redirect("~/BuyWithAgreement.aspx?Items=" + guidKey + "&AcceptUrl=" + acceptUrl + "&DeclineUrl=" + declineUrl);
 
                     Response.Redirect("~/BuyWithAgreement.aspx?Items=" + guidKey + "&AcceptUrl=" + acceptUrl + "&DeclineUrl=" + declineUrl);
 
                 }
 
                 }
 
 
                 //ADD ITEM TO BASKET
 
                 //ADD ITEM TO BASKET
 
                 Basket basket = Token.Instance.User.Basket;
 
                 Basket basket = Token.Instance.User.Basket;
Line 61: Line 60:
 
                     Response.Redirect("~/ProductAccessories.aspx?ProductId=" + basketItem.ProductId + "&ReturnUrl=" + returnUrl);
 
                     Response.Redirect("~/ProductAccessories.aspx?ProductId=" + basketItem.ProductId + "&ReturnUrl=" + returnUrl);
 
                 }
 
                 }
 
 
                 // IF BASKET HAVE SOME VALIDATION PROBLEMS MOVE TO BASKET PAGE
 
                 // IF BASKET HAVE SOME VALIDATION PROBLEMS MOVE TO BASKET PAGE
 
                 List<string> basketMessages;
 
                 List<string> basketMessages;
Line 69: Line 67:
 
                     Response.Redirect(NavigationHelper.GetBasketUrl());
 
                     Response.Redirect(NavigationHelper.GetBasketUrl());
 
                 }
 
                 }
 
 
                 //IF THERE IS NO REGISTERED BASKET CONTROL, WE MUST GO TO BASKET PAGE
 
                 //IF THERE IS NO REGISTERED BASKET CONTROL, WE MUST GO TO BASKET PAGE
 
                 if (!PageHelper.HasBasketControl(this.Page)) Response.Redirect(NavigationHelper.GetBasketUrl());
 
                 if (!PageHelper.HasBasketControl(this.Page)) Response.Redirect(NavigationHelper.GetBasketUrl());
Line 76: Line 73:
 
     }
 
     }
 
</script>
 
</script>
 
 
<asp:Label ID="Label1" runat="server" Text="Add products to cart from affliates site." Font-Bold="true"></asp:Label>
 
<asp:Label ID="Label1" runat="server" Text="Add products to cart from affliates site." Font-Bold="true"></asp:Label>
  
 
Now just put this newly created user control on the page which will be refereed by the external entity and provide the URL as described above over the external sites.
 
Now just put this newly created user control on the page which will be refereed by the external entity and provide the URL as described above over the external sites.

Revision as of 13:41, 18 July 2008

This functionality can be easily incorporated into AbleCommerce store. For example there may be some situation in which you want an affiliate or an external entity to allow customers to add products to the cart from their website.

This can be done with two things, a special link which will be placed over the external entity website and will contain the ProductId in it and secondly a user control which will be on the web page refereed by the that special link. In fact this user control will contain the code to add the product to basket. The user control is not a necessary you can even write the code inside the actual page which will be referred by the special link. The main point of using the user control is that you can use the control on any of the AbleCommerce pages where ever you need that by just modifying the appropriate scriptlet.

The format of special URL will be as bellow

http://yourstoreurl/AddToBasket.aspx?ProductId=1

Now the User control which will be responsible for reading product information from the link and adding product to the cart must be something like

<%@ Control Language="C#" ClassName="AddToCartEx" %> <script runat="server">

   private int _ProductId;
   protected void Page_Load(object sender, EventArgs e)
   {
       _ProductId = PageHelper.GetProductId();
       if (_ProductId > 0)
           AddToCart();
   }
   protected void AddToCart()
   {
       //GET THE PRODUCT ID FROM THE URL
       Product product = ProductDataSource.Load(_ProductId);
       if (product != null)
       {
           string lastShoppingUrl = NavigationHelper.GetLastShoppingUrl();
           if (product.HasChoices)
           {
               //CANT ADD DIRECTLY TO BASKET, SEND TO MORE INFO
               Response.Redirect(product.NavigateUrl);
           }
           BasketItem basketItem = BasketItemDataSource.CreateForProduct(_ProductId, 1);
           if (basketItem != null)
           {
               // 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
                   List<BasketItem> basketItems = new List<BasketItem>();
                   basketItems.Add(basketItem);
                   string guidKey = Guid.NewGuid().ToString("N");
                   Cache.Add(guidKey, basketItems, 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(Page.ResolveClientUrl(product.NavigateUrl)));
                   Response.Redirect("~/BuyWithAgreement.aspx?Items=" + guidKey + "&AcceptUrl=" + acceptUrl + "&DeclineUrl=" + declineUrl);
               }
               //ADD ITEM TO BASKET
               Basket basket = Token.Instance.User.Basket;
               basket.Items.Add(basketItem);
               basket.Save();
               //Determine if there are associated Upsell products
               if (basketItem.Product.GetUpsellProducts(basket).Count > 0)
               {
                   //redirect to upsell page
                   string returnUrl = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Request.Url.ToString()));
                   Response.Redirect("~/ProductAccessories.aspx?ProductId=" + basketItem.ProductId + "&ReturnUrl=" + returnUrl);
               }
               // IF BASKET HAVE SOME VALIDATION PROBLEMS MOVE TO BASKET PAGE
               List<string> basketMessages;
               if (!basket.Validate(out basketMessages))
               {
                   Session.Add("BasketMessage", basketMessages);
                   Response.Redirect(NavigationHelper.GetBasketUrl());
               }
               //IF THERE IS NO REGISTERED BASKET CONTROL, WE MUST GO TO BASKET PAGE
               if (!PageHelper.HasBasketControl(this.Page)) Response.Redirect(NavigationHelper.GetBasketUrl());
           }
       }
   }

</script> <asp:Label ID="Label1" runat="server" Text="Add products to cart from affliates site." Font-Bold="true"></asp:Label>

Now just put this newly created user control on the page which will be refereed by the external entity and provide the URL as described above over the external sites.