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

Forums / Developing with Sitefinity / Change product price during checkout.

Change product price during checkout.

8 posts, 1 answered
  1. David
    David avatar
    29 posts
    Registered:
    28 Apr 2009
    20 Jan 2014
    Link to this post
    I am working on a payment portal using SiteFinity 6.3 and am having trouble with the checkout process. When a user comes to the site they are shown a list of statements that they owe money on which is pulled from an external data source. They are then allowed to select a statement and click to pay. I have created a service type product without inventory tracking in the eCommerce module that I add to the cart when they click to pay a statement. All of that works great except that when i try to set the price of the product while i add it to the cart it doesn't get saved. My add to cart method looks like this:

    public void AddProductToCart(Product product, int qty = 1, OptionsDetails options = null, string currency = null)
            {
                if (options == null)
                    options = new OptionsDetails();
                var cartOrder = GetShoppingCartForUser();
                var cartDetail = cartOrder.Details.SingleOrDefault(d => d.ProductId == product.Id);
                if (cartDetail != null)
                {
                    OrdersManager.IncreaseQuantity(cartDetail.Id, qty);
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(currency))
                        cartOrder.Currency = currency;
                    else
                        cartOrder.Currency = Config.Get<EcommerceConfig>().DefaultCurrency;
                    OrdersManager.AddToCart(cartOrder, product, options, qty);
                    cartDetail = cartOrder.Details.SingleOrDefault(d => d.ProductId == product.Id);
                }
                cartDetail.Price = 33; 
                OrdersManager.SaveChanges();
            }

    Is there something else i need to do in order to save the price back to the cart?

    Thanks
  2. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    Registered:
    07 Dec 2016
    22 Jan 2014
    Link to this post
    Hi David,

    I have tested this code snippet and the value of card price after .SaveChanges() is changed:
    public void AddToCart()
    {
        var ordersManager = OrdersManager.GetManager();
        var catManager = CatalogManager.GetManager();
     
        var product = catManager.GetProducts().FirstOrDefault();
     
        int quantity = 1;
     
        try
        {
            CartOrder shoppingCart = GetShoppingCartForUser(ordersManager);
            CartDetail cartDetail = shoppingCart.Details.Where(d => d.ProductId == product.Id).SingleOrDefault();
     
            if (cartDetail != null)
            {
                ordersManager.IncreaseQuantity(cartDetail.Id, quantity);
            }
            else
            {
                shoppingCart.Currency = Config.Get<EcommerceConfig>().DefaultCurrency;
     
                var options = new OptionsDetails();
     
                options.TotalDeltaPrice = 8;
     
                ordersManager.AddToCart(shoppingCart, product, options, quantity);
            }
            cartDetail = shoppingCart.Details.Where(d => d.ProductId == product.Id).SingleOrDefault();
             
            cartDetail.Price = 33;
            ordersManager.SaveChanges();
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException(Res.Get<OrdersResources>("NotAddedToCart"));
        }
    }
     
    public Guid GetShoopingCartId()
    {
        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);
    }
     
    public CartOrder GetShoppingCartForUser(OrdersManager ordersManager)
    {
        Guid shoppingCartId = GetShoopingCartId();
        CartOrder shoppingCart = ordersManager.TryGetCartOrder(shoppingCartId);
     
        if (shoppingCart == null)
        {
            shoppingCartId = Guid.NewGuid();
            shoppingCart = ordersManager.CreateCartOrder(shoppingCartId, null);
        }
     
        return shoppingCart;
    }

    You could check if it works for you. Similar discussion could be found on that forum post.

    Regards,
    Svetoslav Manchev
    Telerik
    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
    Answered
  3. David
    David avatar
    29 posts
    Registered:
    28 Apr 2009
    22 Jan 2014
    Link to this post
    Using the OptionsDetails.TotalDeltaPrice property instead of the Product.Price property fixed my problem. Thank you very much.

    David
  4. David Lamb
    David Lamb avatar
    10 posts
    Registered:
    02 Oct 2009
    08 Feb 2014
    Link to this post
    I found that this did not automatically update the cart totals; I had to do that manually, and (only then) it worked.

    CartDetail cd = ordersManager.AddToCart(shoppingCart, product, optionsDetails, quantity);
    shoppingCart.PreTaxTotal += cd.Total;
    shoppingCart.PreTaxPrice += cd.Total; // unsure of this property. My quantity is always 1.
    shoppingCart.SubTotalDisplay += cd.Total;
    shoppingCart.ExchangeRatePreTaxTotal += cd.Total;
    shoppingCart.ExchangeRateSubTotal += cd.Total;
    shoppingCart.ExchangeRateSubTotalDisplay += cd.Total;
    shoppingCart.ExchangeRateTotal += cd.Total;
    shoppingCart.Total += cd.Total;
    ordersManager.SaveChanges();
  5. David Lamb
    David Lamb avatar
    10 posts
    Registered:
    02 Oct 2009
    08 Feb 2014
    Link to this post
    P.S. AddToCart, combined with CartDetail.TotalDeltaPrice is EXTREMELY useful for trying to have a donation mechanism in SF 6.3, because the published Donation widget is no longer compatible, and while SF has a built-in donation capability in their roadmap, it's not here yet.
  6. David
    David avatar
    29 posts
    Registered:
    28 Apr 2009
    10 Feb 2014 in reply to David Lamb
    Link to this post
    That's good to know ill have to do some more testing on my totals. Have you by chance tried to change the title or description of a product while adding it to the cart? I have a need to pass a customer id on to paypal standard in 6.3 and have not found any way of doing this. 
  7. Nayden Gochev
    Nayden Gochev avatar
    32 posts
    Registered:
    03 Nov 2014
    10 Feb 2014
    Link to this post
    Hi David,

    Have you tried to set the Title of the CartDetail?

    In order to be localizable we have a new class LString which contains the title on each locale.
    So during the Add To Cart we do this :

    foreach (var culture in product.Title.GetAvailableLanguages())
            {
                cartDetail.Title.SetString(culture, product.Title.GetString(culture, false));
            }

    This is connected to all cultures so language widget and ML pages to work correctly. Of course you can append whatever you want to the end of it like a custom ids.

    I didn't understood fully the customer Id part....but If you use our implementation of Paypal standard currently we indeed pass only the email of the customer but not the ID of the customer.

    If you need to pass additional information to paypal you may do the following:

    - Create a new class extending PayPalStandardProvider.
    - Override the  NameValueCollection GetPostValues(IPaymentRequest) method.
    - Inside this method invoke first our base.GetPostValues(request); method.
    - Then to the result.. add whatever you want to this NameValueCollection. You can add any parameters that paypal standard understand and return the modified collection after that
    - Go to Administration >> Settings >> Advanced >> PaymentProcessor >> PaymentProcessorProviders >> PAYPAL_STANDARD and change the value ProviderType (last textbox) to point to your new DavidPayPallStandardProvider and Save Changes

    From now on : one additional parameter ( the one you added) will be passed to PayPal Standard each time paypal standard is opened ( you can see this parameter via firebug or using Telerik Fiddler for example )

    Regards,
    Nayden Gochev
    Telerik
    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
  8. David
    David avatar
    29 posts
    Registered:
    28 Apr 2009
    10 Feb 2014 in reply to Nayden Gochev
    Link to this post
    That's very helpful, i should have assumed i could override the paypal provider. Thank you
8 posts, 1 answered