Page 1 of 1

Persist Order-Id in cookie?

Posted: Fri Jul 10, 2015 1:28 am
by rudy
Hi,

I was planning to persist the order-id in cookie, so that when the user closes the browser and re-opens, the cart should display previously added items.
For a new user, i managed to create a cookie by overriding createNewCartForCustomer(Customer customer) method of class OrderServiceImpl.

Code: Select all

@Override
   public Order createNewCartForCustomer(Customer customer) {
   //Broadleaf flow
        Order order = super.createNewCartForCustomer(customer);
        // #### Customization - Create Cookie ###
        Cookie cookie = getCookieTools().create(CookieTools.ORDER_COOKIE, String.valueOf(order.getId()),   CookieTools.COOKIE_MAX_AGE);
        getCookieTools().add(cookie);
        return order;
   }


The cookie is creating fine on every new user.

Now, the challenge is loading the order on browser re-open, when cookie is present.
What i tried was to override public Order reloadOrder(Order order) method of OrderServiceImpl class.

Code: Select all

   @Override
   public Order reloadOrder(Order order) {
      
      //Customization for reading cookie
      Long orderId = getOrderIdFromOrderCookie();
      if(orderId != null) {
         return orderDao.readOrderById(orderId, true);
      }
      
      //IF ORDER ID is not found in cookie, Broadleaf flow follows
      return super.reloadOrder(order);
   }


Now, the challenge is the current order is fetched from the static method getCart() of class CartState.
I wanted to override this method and customize it to use reloadOrder method if cookie is found.
Since, static methods are a property of class, and the whole broadleaf code refers it from expression Cart.getState, extending it wont help me.


Any pointers to the problem?

Re: Persist Order-Id in cookie?

Posted: Mon Jul 27, 2015 4:35 am
by phillipuniverse
You want to override what actually invokes CartState.getCart() which is the CartStateRequestProcessor. The default behavior is to look up the current customer (from CustomerState) and then populate CartState based off of the IN_PROCESS order for the Customer.

I would actually recommend storing the CustomerId in the cookie instead and overriding the CustomerStateRequestProcessor. Once you do that, the Order will be looked up automatically anyway.

Security could be an issue here though, because if you are just storing CustomerId (or even OrderId) as a cookie, someone could simply overwrite the cookie to a different value.

Re: Persist Order-Id in cookie?

Posted: Fri Jul 31, 2015 11:41 am
by rudy
I will definitely try this and get back to you.

Also, i planned on storing order-id only for anonymous users.
If the order-id belongs to a customer who is registered, the order-id from cookie will not be picked-up, and normal flow would follow. Therefore, the security flaw might not be there.

Let me know if you think otherwise or have better ideas. :)

Re: Persist Order-Id in cookie?

Posted: Fri Jul 31, 2015 12:29 pm
by phillipuniverse
Gotcha, I think that makes good sense. Yeah the CartStateRequestProcessor seems like the right place to override/extend.