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

From AbleCommerce Wiki
Jump to: navigation, search
m
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
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.
+
[[Category:AbleCommerce 7]]
  
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 functionality to add products to Ablecommerce cart from an external entity can be easily implemented. This feature may be required in certain situations, for example, allowing an affiliate to add products to the cart from his/her website.
  
The format of special URL will be as bellow
+
The external entity can link to a Ablecommerce page with special parameters in the URL indicating the product to be added to the basket and its quantity. On the store side a custom control can be implemented to handle such requests. A custom user control is not necessary; code can be written inside the actual page which will be referred by the incoming link. The main point of using the user control is that you can use the control on any of the pages where ever you need this functionality.
  
 +
The format of special URL will be as bellow
 +
<code>
 +
<pre>
 
http://yourstoreurl/AddToBasket.aspx?ProductId=1
 
http://yourstoreurl/AddToBasket.aspx?ProductId=1
 +
</pre>
 +
</code>
  
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  
+
The user control which will be responsible for reading product information from the URL and adding product to the cart may look something like this
  
 +
<code>
 +
<pre>
 
<%@ Control Language="C#" ClassName="AddToCartEx" %>
 
<%@ Control Language="C#" ClassName="AddToCartEx" %>
 
<script runat="server">
 
<script runat="server">
Line 73: Line 80:
 
</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>
 +
</pre>
 +
</code>
  
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.
+
Put this newly created user control on the page which will be refereed by the external entity and provide the URL as described above at the external sites and you are done.

Latest revision as of 11:02, 15 August 2013


The functionality to add products to Ablecommerce cart from an external entity can be easily implemented. This feature may be required in certain situations, for example, allowing an affiliate to add products to the cart from his/her website.

The external entity can link to a Ablecommerce page with special parameters in the URL indicating the product to be added to the basket and its quantity. On the store side a custom control can be implemented to handle such requests. A custom user control is not necessary; code can be written inside the actual page which will be referred by the incoming link. The main point of using the user control is that you can use the control on any of the pages where ever you need this functionality.

The format of special URL will be as bellow

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

The user control which will be responsible for reading product information from the URL and adding product to the cart may look something like this

<%@ 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>

Put this newly created user control on the page which will be refereed by the external entity and provide the URL as described above at the external sites and you are done.