Shopping Cart cookie expiration
Is there anyway to modify the expiration date on the shopping cart cookie set by the ecommerce module?
Hello Oliver,
You can get the cookie and manage it as per your needs. How to get the cookie could be found in that discussion (in GetShoopingCartId() method):
http://www.sitefinity.com/developer-network/forums/developing-with-sitefinity-/programatically-add-product-to-cart
I hope this helps.
Regards,
Svetoslav Manchev
Telerik
Thanks for your response.
So it looks like if I want to change the cookie expiration date, I would have to create a custom AddToCart widget and also a custom OrdersManager class, is that correct?
Shame the expiration date value is hard-coded , would be a useful editable setting...
Hello Oliver,
You can subscribe to any of the Ecommece events and manage the cookie there:
http://docs.sitefinity.com/for-developers-ecommerce-events
Regards,
Svetoslav Manchev
Telerik
The shopping cart cookie is created when a user first adds an item to their basket. Which of those events would fire for that? They all seem to be based around the order being placed and payment taken.
Thanks.
Hello,
As the listed events are not suitable for you can use the ViewMap functionality to extend the AddToCartWidget:
http://www.sitefinity.com/developer-network/knowledge-base/details/how-to-extend-the-view-of-building-widget-using-viewmap
and you can add in its code behind the logic you need, extending the default behaviour.
The HostType that you need to set is:
Telerik.Sitefinity.Modules.Ecommerce.Orders.Web.UI.AddToCartWidget
Alternatively you can create control inheriting from AddToCartWidget and overriding for example the "AddToCartButton_Command" method to implement the logic you need after the base functionality is completed. Than you can change the default control with your custom one in the details view template of the products.
You can use the same approach for any control you need.
I hope this helps.
Regards,
Svetoslav Manchev
Telerik
It seems overkill to have to alter the whole Add to Cart process just to amend the cookie expiration. I decided to add an HttpModule to intercept the response when the cookie was created and amend the expiration:
public
class
AmendShoppingCartCookieModule : IHttpModule
public
void
Dispose()
// nothing to do.
public
void
Init(HttpApplication context)
context.EndRequest +=
(
new
EventHandler(
this
.ApplicationEndRequest));
private
void
ApplicationEndRequest(
object
source, EventArgs e)
// If the request is setting the shopping cart cookie
var currentContext = HttpContext.Current;
if
(!currentContext.Response.Cookies.AllKeys.Contains(
"shoppingCartId"
) )
return
;
// and the cookie expiration date is more than 2 days
var cartCookie = currentContext.Response.Cookies[
"shoppingCartId"
];
if
(cartCookie.Expires.Date <= DateTime.Now.AddDays(2).Date)
return
;
// update the cookie expiration
var newCartCookie =
new
HttpCookie(
"shoppingCartId"
, cartCookie.Value)
Expires = DateTime.Now.AddDays(2)
;
currentContext.Response.Cookies.Set(newCartCookie);
and added the module in my web.config
<system.webServer>
<modules runAllManagedModulesForAllRequests=
"true"
>
<remove name=
"AmendShoppingCartCookieModule"
/>
<add name=
"AmendShoppingCartCookieModule"
type=
"MyProject.Web.AmendShoppingCartCookieModule, MyProject.Web"
/>
Hi Oliver,
Thank you for the shared solution with the community.
Regards,
Svetoslav Manchev
Telerik