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

Forums / Ecommerce / Shopping Cart cookie expiration

Shopping Cart cookie expiration

8 posts, 1 answered
  1. Ollie P
    Ollie P avatar
    9 posts
    Registered:
    06 Jan 2016
    18 Jan
    Link to this post
    Is there anyway to modify the expiration date on the shopping cart cookie set by the ecommerce module?
  2. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    Registered:
    29 Nov 2016
    21 Jan
    Link to this post
    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
     
    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
     
  3. Ollie P
    Ollie P avatar
    9 posts
    Registered:
    06 Jan 2016
    25 Jan
    Link to this post

    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...

  4. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    Registered:
    29 Nov 2016
    28 Jan
    Link to this post
    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
     
    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
     
  5. Ollie P
    Ollie P avatar
    9 posts
    Registered:
    06 Jan 2016
    28 Jan in reply to Svetoslav Manchev
    Link to this post

    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.

  6. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    Registered:
    29 Nov 2016
    28 Jan
    Link to this post
    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
     
    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
     
  7. Ollie P
    Ollie P avatar
    9 posts
    Registered:
    06 Jan 2016
    02 Feb
    Link to this post

    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" />

    Answered
  8. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    Registered:
    29 Nov 2016
    02 Feb
    Link to this post
    Hi Oliver,

    Thank you for the shared solution with the community.

    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
8 posts, 1 answered