Change product price during checkout.
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
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;
Using the OptionsDetails.TotalDeltaPrice property instead of the Product.Price property fixed my problem. Thank you very much.
David
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();
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.
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.
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));
That's very helpful, i should have assumed i could override the paypal provider. Thank you