Skip to content

Commit

Permalink
Merge pull request #30 from Kentico/@fix/fixed_invalid_shopping_cart_…
Browse files Browse the repository at this point in the history
…cache

Fixed invalid shopping cart cache
  • Loading branch information
Rumec authored Dec 6, 2024
2 parents a336f80 + 4565593 commit 3cf3965
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
17 changes: 16 additions & 1 deletion examples/DancingGoat-K13Ecommerce/Controllers/TestController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Kentico.Xperience.K13Ecommerce.Orders;
using Kentico.Xperience.K13Ecommerce.Products;
using Kentico.Xperience.K13Ecommerce.ShoppingCart;
using Kentico.Xperience.K13Ecommerce.StoreApi;

Expand Down Expand Up @@ -55,7 +56,7 @@ public async Task<IActionResult> TestUpdateOrder([FromServices] IOrderService or

order.OrderShippingOption = new KShippingOption { ShippingOptionId = 2 };
order.OrderPaymentOption = new KPaymentOption { PaymentOptionId = 1 };

order.OrderPaymentResult = new KPaymentResult
{
PaymentIsCompleted = true,
Expand All @@ -67,5 +68,19 @@ public async Task<IActionResult> TestUpdateOrder([FromServices] IOrderService or

return Ok();
}

public async Task<IActionResult> TestCartCacheIssue([FromServices] IShoppingService shoppingService, [FromServices] IProductService productService)
{
//first ensure that ShoppingCartGUID cookie is set to existing cart from previous session

var prices = await productService.GetProductPrices(33);//set real SKUID
// now empty cart is cached in K13

var cart = await shoppingService.GetCurrentShoppingCartContent();

// now when cart is empty and cookie is removed -> invalid

return Json(cart);
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using IdentityModel.Client;

using Kentico.Web.Mvc;
using Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;
using Kentico.Xperience.K13Ecommerce.Activities;
using Kentico.Xperience.K13Ecommerce.Admin;
Expand Down Expand Up @@ -45,6 +46,10 @@ public static IServiceCollection AddKenticoStoreServices(this IServiceCollection
{
services.AddOptions<KenticoStoreConfig>().Bind(configuration.GetSection("CMS" + nameof(KenticoStoreConfig)));

services.Configure<CookieLevelOptions>(options =>
options.CookieConfigurations.Add(ShoppingCartClientStorage.ShoppingCartKey, CookieLevel.Essential)
);

// default cache for token management
services.AddDistributedMemoryCache();
//add token management config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Kentico.Xperience.K13Ecommerce.ShoppingCart;
internal class ShoppingCartClientStorage
(ICookieAccessor cookieAccessor, IConversionService conversionService) : IShoppingCartClientStorage
{
private const string ShoppingCartKey = "ShoppingCartGUID";
public const string ShoppingCartKey = "ShoppingCartGUID";


/// <inheritdoc/>
Expand All @@ -19,7 +19,13 @@ internal class ShoppingCartClientStorage
/// <inheritdoc/>
public void SetCartGuid(Guid cartGuid) =>
cookieAccessor.Set(ShoppingCartKey, cartGuid.ToString(),
new CookieOptions { HttpOnly = true, Expires = DateTimeOffset.Now.AddMonths(1), SameSite = SameSiteMode.Strict });
new CookieOptions
{
IsEssential = true,
HttpOnly = true,
Expires = DateTimeOffset.Now.AddMonths(1),
SameSite = SameSiteMode.Strict
});


/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,19 @@ public void SetCurrentShoppingCart(ShoppingCartInfo cart)
/// <returns>Candidate cart or <c>null</c> when no candidate cart found.</returns>
protected ShoppingCartInfo GetCandidateCart(ref bool anonymize, ref bool evaluationIsNeeded)
{
//custom code starts
var shoppingCartGuid = GetCurrentCartGuid();

var cart = mCartCache.GetCart();
if (cart != null)
if (cart != null && (shoppingCartGuid == Guid.Empty || cart.ShoppingCartGUID != Guid.Empty))
{
// Return cached cart only when requested ShoppingCartGuid is empty otherwise cached cart cannot be empty (ShoppingCartGUID is empty GUID).
// Cache can contain empty cart from another request where cart identifier is not sent, like request for price calculation which works with empty cart
return cart;
}

evaluationIsNeeded = true;

//custom code starts
var shoppingCartGuid = GetCurrentCartGuid();
cart = shoppingCartGuid != Guid.Empty ? mCartRepository.GetCart(shoppingCartGuid) : null;
//Don't anonymize cart because getting cart from client is only way how to get it when it isn't cached.
//On client app (xbyK) session storage is used as primary option and cookie as secondary, so some anonymize flag in REST api
Expand Down

0 comments on commit 3cf3965

Please sign in to comment.