Page 1 of 1

Affiliate Add to Cart?

Posted: Wed Apr 23, 2008 9:27 am
by DFresh
Is there a way for an affiliate or an external entity to allow customers to add products to the cart from their website? For instance, a customer wants to buy a product that I carry but they are on an affiliate's website. When they click add to cart, it takes them to my shopping cart.

Thanks in advance!

Re: Affiliate Add to Cart?

Posted: Mon May 05, 2008 2:48 am
by m_plugables
I don't think so that currently any page is providing this facility. But this can be incorporated into ac7 very easily with some small changes. You can create some user control which can handle this type of functionality when placed over some page.

Re: Affiliate Add to Cart?

Posted: Mon May 05, 2008 2:57 am
by m_plugables
Create a new file ConLib/AddToCartEx.ascx and put the following code in it.

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 || product.UseVariablePrice)
            {
                //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 place this control on some page like any normal ConLib control. And place the URL of this page on the site from where you want to send the product to cart. The URL must be contain the Product Id of the desired product. For example

Code: Select all

http://www.somesite.com/mypage.aspx?ProductId
I hope so this sample will help you.

Re: Affiliate Add to Cart?

Posted: Mon Aug 25, 2008 5:18 pm
by dtr18c
Hi,

I'm new to AbleCommerce and am trying to make use of the external add to cart functionality.
I've added the user control and added the conlib referenc on a page but when i visit the page it redirects me to the product page and doesn't add anything to the basket.

I'm not quite sure what additional information I should provide in this case.
If anybody has gotten this to work or can help out let me know what other information I can provide to troubleshoot this.

Thanks,
Chad

Re: Affiliate Add to Cart?

Posted: Mon Aug 25, 2008 6:31 pm
by jmestep
Do your products have options? It won't work in that case.

Re: Affiliate Add to Cart?

Posted: Tue Aug 26, 2008 9:53 am
by dtr18c
Thanks for pointing me in the right direction Judy.
I was originally testing this with a kit product.

Is there any way paramaters could be constructed for kit/option items?

Re: Affiliate Add to Cart?

Posted: Tue Aug 26, 2008 1:09 pm
by jmestep
There probably are, since it could be done in Able 5. I just haven't experimented, but you could try adding a kit to the basket from the normal product page and putting code on the basket page to display the parameters it is using.