+1-888-365-2779
Try Now
More in this section

Forums / Ecommerce / Using ecommerce for donations and adding items to cart programatically

Using ecommerce for donations and adding items to cart programatically

15 posts, 0 answered
  1. SteveV
    SteveV avatar
    178 posts
    Registered:
    06 Nov 2011
    28 Nov 2011
    Link to this post
    We're a special needs school and have been using paypal for donations.  I'd like to use Sitefinity's ecommerce module to manage donations using Authorize.NET.  Is it possible to do this?

    Also, is there a way to add a product to the cart from code?

    Thanks--Steve
  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    08 Dec 2016
    01 Dec 2011
    Link to this post
    Hi Steve,

    Here is a blog post that explains how to add custom payment provider. Some payment providers already ship with Sitefinity Ecommerce (Authorize.Net, PayPal PayFlow, SagePay).

    Greetings,
    Stanislav Velikov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. SteveV
    SteveV avatar
    178 posts
    Registered:
    06 Nov 2011
    01 Dec 2011
    Link to this post
    Hi Stanislav,

    Thanks for the reply.  I have a payment provider setup and working so that's not the issue.  The problem is how do I create a product with a variable price?  In other words; it doesn't make sense to create a donation "product" for $25, $50, $75, $100, etc.  Also, imagine we stop at a thousand and a very generous person decides they want to donate $2500.  It would be silly to make them add two $1000 donation "products" and one $500 donation "product". 

    I'm looking for a way to create a product that allows the user to enter a dollar amount before adding the item to the cart.

    Thanks -- Steve
  4. SteveV
    SteveV avatar
    178 posts
    Registered:
    06 Nov 2011
    05 Dec 2011
    Link to this post
    Anyone?

    Thanks -- Steve
  5. Grace Hallwachs
    Grace Hallwachs avatar
    144 posts
    Registered:
    03 Nov 2014
    06 Dec 2011
    Link to this post

    Hello Stevev,

    Thank you for contacting support. I apologize for the delay in getting back you. I was consulting with some of the developers about a workaround for donations.

    We will be adding a donations module in a future release but I do have a workaround for you to use now.

    I have included a video and attached a code sample that shows how you would setup donations. We are using an external template based on the default Product Detail View page. This custom template has a donations textbox implemented and you can modify it per your requirements.

    I also have submitted a feature request on your behalf.

    I am sorry for any inconvenience this causes you. Please do not hesitate to let us know if you have anymore questions or concerns.

    All the best,

    Best regards,
    Grace Hallwachs
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. SteveV
    SteveV avatar
    178 posts
    Registered:
    06 Nov 2011
    06 Dec 2011
    Link to this post
    Hi Grace,

    Thanks for this.  I'll give it a try and report back.

    --Steve
  7. SteveV
    SteveV avatar
    178 posts
    Registered:
    06 Nov 2011
    08 Dec 2011
    Link to this post
    Hi Grace,

    I've played around with the sample code you provided and everything works fine until you try editing items in the shopping cart.  Updating the cart causes the donation item subtotals to revert to thier original values.  For instance, the shopper adds a donation item and enters an amount of $500.  Next they add a tee shirt to the cart.  Looking at their cart they decide to change the quantity of the tee shirt to 2 and click the update button.  The result: the tee shirt item quantity is updated but the donation item reverts to $0 (or whatever the item's real product price is).
    I have attached a couple of screenshots.

    Any thoughts on how we might workaround this?

    Thanks -- Steve
  8. SteveV
    SteveV avatar
    178 posts
    Registered:
    06 Nov 2011
    09 Dec 2011
    Link to this post
    More info:

    As soon as checkout is initiated the donation values revert to $0.  I'm assuming that the ECommerce module does another product price lookup and calculation before displaying the checkout pages.  As you might guess, this one's a show stopper.  Any ideas?

    Thanks--Steve
  9. Grace Hallwachs
    Grace Hallwachs avatar
    144 posts
    Registered:
    03 Nov 2014
    13 Dec 2011
    Link to this post
    Hello Stevev,

    My apologies for inconvenience this issue has caused.

    I am currently working with a developer to come up with a possible workaround. I will let you know as soon as possible the outcome.

    All the best,

    Grace Hallwachs
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  10. SteveV
    SteveV avatar
    178 posts
    Registered:
    06 Nov 2011
    15 Dec 2011
    Link to this post
    One other thing I noticed that appears to be a bug in the Product.Description property.  It has get and set accessors but changes to the Description property aren't persisted when the item is added to the cart.

    Here's what I'm doing:

    I created a donation widget (see attached image)  that has a combobox that allows the user to select a donation category such as

    General Donation
    2011 Capital Campaign
    2011 Bike-a-thon
    Other

    The user enters a donation amount and clicks the "Make a Donation" button which adds the donation to the cart.  I'm modifyin the product description in the codebehind to include the donation type.  So, if the user selects the "2011 Capital Campaign" donation category, the description is modifed to "Donation: 2011 Capital Campaign".  I can modify the Product.Description property but the value reverts back the the original value when the item is added to the cart.

    Fixing this would eliminate the need to have a seperate donation "product" for every possible donation type and would allow the user to specify the reason for the donation such as "In memory of John Smith".

    Thanks -- Steve
  11. Grace Hallwachs
    Grace Hallwachs avatar
    144 posts
    Registered:
    03 Nov 2014
    21 Dec 2011
    Link to this post
    Hello Stevev,

    If possible, could you open a support ticket and send us your code? We may be able to further assist you in a support ticket.

    Also, are you using the Catalog manager to get products? I have included some documentation to the Developer's guide that further explains the CatalogManager.

    Concerning modifying the product description in the code behind to include the donation type, changing this behavior wouldn't necessarily resolve the issue. For example, if you have multiple users adding donation products to their carts changing the product description each time would cause it to override another person's cart.

    Please let us know if you have anymore questions or concerns.

    Greetings,
    Grace Hallwachs
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  12. SteveV
    SteveV avatar
    178 posts
    Registered:
    06 Nov 2011
    21 Dec 2011
    Link to this post
    Hi Grace,

    Thanks for the follow-up.  The problem I reported with the donation amount reverting back to the original product price (in my case $0) appears to have been fixed in version 4.4 so thanks for that :)

    Regarding modifying the product description by adding the donation type; not sure I understand the rationale.  The Product.Description property has "get" and "set" accessors.  If this property is readonly, clearly it should not have a "set" accessor.  Also, if modifying the donation product price does not affect donation products in other users' carts, why should the description property behave any differently?

    Am I not usderstanding this correctly?

    Here's how I'm adding a donation to the cart:
    public static bool AddDonation(decimal donationAmount, string donationType)
    {
        string productSKU = "DON-0000-0001";
        var ordersManager = OrdersManager.GetManager();
        var catManager = CatalogManager.GetManager();
        bool result = false;
     
        Product product = catManager.GetProduct(productSKU);
     
        if (product == null)
            return false;
     
        try
        {
            product.Price = donationAmount;
     
            if (donationType.Length > 0)
                product.Description = string.Format("Donation: {0}", donationType);
     
            CartOrder shoppingCart = GetShoppingCartForUser(ordersManager);
     
            CartDetail cartDetail = null;
            List<CartDetail> donationItems = shoppingCart.Details.Where(d => d.ProductId == product.Id).ToList();
     
            if (donationItems != null && donationItems.Count > 0)
                cartDetail = donationItems[0];
     
            else
                cartDetail = shoppingCart.Details.Where(d => d.ProductId == product.Id).SingleOrDefault();
     
            if (cartDetail != null)
            {
                decimal newPrice = cartDetail.Price + donationAmount;
                cartDetail.Price = newPrice;
                result = true;
            }
     
            else
            {
                shoppingCart.Currency = Config.Get<EcommerceConfig>().DefaultCurrency;
                //ordersManager.AddToCart(shoppingCart, product, qty);
     
                OptionsDetails optionsDetails = new OptionsDetails();
                optionsDetails.Sku = productSKU;
                ordersManager.AddToCart(shoppingCart, product, optionsDetails, 1);
                result = true;
            }
     
            if (result)
                ordersManager.SaveChanges();
     
            return result;
        }
     
        catch (Exception ex)
        {
            throw new InvalidOperationException(Res.Get<OrdersResources>("NotAddedToCart"));
        }
    }
     
    public static CartOrder GetShoppingCartForUser(OrdersManager ordersManager)
    {
        Guid shoppingCartId = GetShoppingCartId();
        CartOrder shoppingCart = null;
     
        // If the shopping cart ID is empty, we need to create a new cookie
        if (shoppingCartId == Guid.Empty)
        {
            shoppingCart = ordersManager.CreateCartOrder();
            shoppingCartId = shoppingCart.Id;
     
            HttpCookie shoppingCartCookie = shoppingCartCookie = new HttpCookie(EcommerceConstants.OrdersConstants.ShoppingCartIdCookieName);  //create a new shopping cart cookie
            DateTime now = DateTime.Now;
             
            // Set the cookie value.
            shoppingCartCookie.Value = shoppingCartId.ToString();
             
            // Set the cookie expiration date.
            shoppingCartCookie.Expires = now.AddMonths(1);
             
            // Add the cookie.
            HttpContext.Current.Response.Cookies.Add(shoppingCartCookie);
        }
     
        else
            shoppingCart = ordersManager.TryGetCartOrder(shoppingCartId);
     
        if (shoppingCart == null)
        {
            shoppingCartId = Guid.NewGuid();
            shoppingCart = ordersManager.CreateCartOrder(shoppingCartId, null);
        }
     
        return shoppingCart;
    }
     
    public static Guid GetShoppingCartId()
    {
        HttpCookie shoppingCartCookie = HttpContext.Current.Request.Cookies[EcommerceConstants.OrdersConstants.ShoppingCartIdCookieName];
     
        if (shoppingCartCookie == null || string.IsNullOrWhiteSpace(shoppingCartCookie.Value))
            return Guid.Empty;
     
        if (!shoppingCartCookie.Value.IsGuid())
            throw new InvalidOperationException("cartOrderId string cannot be parsed as a GUID; please provide a valid cartOrderId value.");
     
        return new Guid(shoppingCartCookie.Value);
    }


    Thanks--Steve
  13. Grace Hallwachs
    Grace Hallwachs avatar
    144 posts
    Registered:
    03 Nov 2014
    22 Dec 2011
    Link to this post
    Hello Steve,

    When you add a product to the cart, the price associated with that product is tied to the cart. For example, once you add the product to your cart, the price of the product that is in the cart stays the same even if another user adds the same product to their cart with a different price.

    The product description is associated with the product not the cart. So each time the product description changes, the product description will override what another person has in their carts.

    Please let us know if you have anymore questions or concerns.

    Regards,
    Grace Hallwachs
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  14. SteveV
    SteveV avatar
    178 posts
    Registered:
    06 Nov 2011
    22 Dec 2011
    Link to this post
    Hi Grace,

    Since the product price is variable depending on factors such as sales, discounts, etc. I guess this makes sense.  What still doesn't make sense is having a "set" accessor for the Product.Description property if it can't be changed?  It just makes things needlessly confusing.

    Thanks--Steve
  15. Venkata Koppaka
    Venkata Koppaka avatar
    67 posts
    Registered:
    24 Sep 2012
    23 Dec 2011
    Link to this post
    Hello Steve,

    You are missing a call to save changes on CatalogManager. Below is what that has to be changed in your sample
    if (donationType.Length > 0)
            {
                product.Description = string.Format("Donation: {0}", donationType);
                catManager.SaveChanges();
            }  

    Or in general you use the snippet below -
    public void ChangeProductDescription(string description)
            {
                CatalogManager manager = CatalogManager.GetManager();
                Product p = manager.GetProducts().First(); //Get the product in any way
                p.Description = description;
                manager.SaveChanges();
            }


    All the best,
    Venkata Koppaka
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
15 posts, 0 answered