UCommerce Prevent Users from adding new items during checkout

201 Views Asked by At

I have an Ecommerce website build with UCommerce. During the checkout process the user will be redirected to the payment portal for the payment.

I want to prevent users from adding new items in the basket while the user is in the payment portal. My current solution is to save the basket to a Session before redirecting the user to the payment portal.

Session["checkoutOrder"] = TransactionLibrary.GetBasket(!TransactionLibrary.HasBasket()).PurchaseOrder;

How can I overwrite the current basket with the one in the Session After the payment? This is to revert the basket to its original state before the payment.

I tried this:

[HttpPost]
public ActionResult ExecutePayment()
{
var order = Session["checkoutOrder"] as PurchaseOrder;
order.Save();
...
}

But I'm getting an error on order.Save():

Batch update returned unexpected row count from update; actual row count: 0; expected: 1
2

There are 2 best solutions below

0
On BEST ANSWER

I'd just add to this as well that your Session["orderInProcess"] is an anti pattern in uCommerce. You may run into nasty exceptions as you're persisting NHibernate entities through requests which can/will lead to Session disposed exceptions. It may also lead to the initial exception that you're experiencing as you're actually by-passing the sesssion state of NHibernate.

Just use TransactionLibrary.GetBasket(!TransactionLibrary.HasBasket()).PurchaseOrder; every time you're retrieving your basket. NHibernate will take care of caching the order for you.

Then you can use order properties to save the state you're in.

var basket = TransactionLibrary.GetBasket(!TransactionLibrary.HasBasket()).PurchaseOrder;
basket["CheckoutInProcess"] = "True";

Best regards Morten

0
On

I handled this differently since I have no way of reverting back the basket to its original state.

I decided to block the user from adding items in the basket when the payment is in process.

I created a session Session["orderInProcess"]=true before I redirect the user to the payment gateway.

Now every time the user tries to add a new item in the basket, I will check first if his current order is in process. like so:

[HttpPost]
public ActionResult AddToBasket(string sku, string quantity, string variant = null)
{

     if (Session["orderInProcess"] != null)
     {
         if (bool.Parse(Session["orderInProcess"].ToString()))
         {
             return Json(new
             {
                  Success = false,
                  ErrorMessage = "Order is currently in process."
             });
          }
      }
   .....
}

I hope this helps.