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

From AbleCommerce Wiki
Jump to: navigation, search
 
Line 1: Line 1:
 +
[[Category:AbleCommerce 7]]
 +
 
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 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.
  

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.