Add product to cart via URL

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
morussell
Ensign (ENS)
Ensign (ENS)
Posts: 3
Joined: Wed Feb 25, 2009 5:40 pm

Add product to cart via URL

Post by morussell » Fri Apr 24, 2009 12:02 pm

I'm trying to add a product to the basket via a URL and then go the the basket. I have tried to using http://yourstoreurl/AddToBasket.aspx?ProductId=1. I copied the code from http://wiki.ablecommerce.com/index.php/ ... nal_entity. I get an item cannot be found error. There has to be an easy way to this.

Code: Select all

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

User avatar
moopa
Lieutenant (LT)
Lieutenant (LT)
Posts: 59
Joined: Tue May 13, 2008 10:11 am

Re: Add product to cart via URL

Post by moopa » Sat Apr 25, 2009 4:28 pm

Hi there.

We are using the AbleCommerces Class Library without any of the front end controls.
I am adding a product in a slightly different way.
Please note that if your product has variants (such as size or color) you will need to add item.OptionList which contains all the OptionChoiceId's.
If you do not add them then you will get an item not available error. I have worked this out from trial and error :)

Code: Select all

            BasketItem item = new BasketItem();
            item.OrderItemType = OrderItemType.Product;
            item.ProductId = CtProduct.ProductId;
            item.Quantity = _quantity;
            item.Price = CtProduct.Price;
            item.Name = CtProduct.Name;
            
            // ADD OPTIONS (EXAMPLE FORMAT - THERE ARE 8 OPTIONS PER PRODUCT IN TOTAL)
            // BELOW 10 = Color and 11 = Size (OptionChoiceId's from the database)
            item.OptionsList = "10,11,0,0,0,0,0,0";



            Basket b = Token.Instance.User.Basket;

            b.Items.Add(item);
            b.Save();

// OTHER CODE AND REDIRECT ETC AFTER SAVE .....


Post Reply