Developer forum

Forum » Development » Handling Stock Updates in Cart V1

Handling Stock Updates in Cart V1


Reply
Hi there,

I noticed that Cart V1 updates the stock in step 6. This is fine when that page gets called correctly, but is problematic when users close their browser and never reach that step.

So, in order to deal with stock accurately, I was wondering about the following:

1. Is there a way to stop step 6 from doing this so I can handle it myself in a payment gateway callback page?

2. If I can't stop it, is there a way to determine if it has been done or not so I can do it myself in the callback page? Is OrderStepNum guaranteed to be 6 when the stock has been updated (and 6 only)? Or should I hook into Order.Steps.Completed and keep track of this in a custom field myself?

Imar


Replies

 
Nicolai Høeg Pedersen
Reply
The Notifications.eCommerce.Order.Steps.Completed is fired the moment after the stock of the product have been updated in step 6. Only way to avoid this, is preventing step 6 to execute or, heres a trick, having a Gateway provider that implements IGatewayCallBack.

If the order is handled by a gateway provider that implements IGatewayCallBack. The gateway provider implements ProcessGatewayCallBack which  calls GatewayProvider.SetOrderComplete in the end which then updates the stock levels.

So overriding SetOrderComplete on your Gateway provider is one way of bypassing it.

That said, I would go with the Notifications.eCommerce.Order.Steps.Completed that updates a custom field the first time it is called marking the order as stock updated. And when that notification has run, the cart is an order with step 6 - and that cannot change.
 
Reply

Hi Nicolai,

I'll promise I'll go for the IGatewayCallBack next time I build my own provider. Hopefully, there's some more docmentation on this subject by that time (or I should write my own at DeVierKoeden.com ;-) )

Just implemented Completed and set a custom field. Then in the callback I see if this field has been set and skip calling SetOrderComplete if it has. For future reference in case anyone is interested, here's the code:

1. NotificationSubscriber
using System;
using System.Linq;
using Dynamicweb.eCommerce.Orders;
using Dynamicweb.Notifications;
using Dynamicweb.Extensibility;

namespace Mediaweb.Sapph.Web.Extenders
{
  [Subscribe(eCommerce.Order.Steps.Completed)]
  public class CheckOutDoneOrderIsComplete : NotificationSubscriber
  {
    public override void OnNotify(string notification, object[] args)
    {
      // Dynamicweb called Step 6, and updated the stock for us.
      // We store this in a custom order field so we can avoid doing this again when the payment gateway callback calls us.
      Order order = args[0] as Order;
      if (order == null)
        throw new Exception("Order is null in CheckOutDoneOrderIsComplete.OnNotify.");

      var stockHasBeenUpdatedField = (from OrderFieldValue f in order.OrderFieldValues
                                      where f.OrderField.SystemName == "StockUpdated"
                                      select f).SingleOrDefault();
      if (stockHasBeenUpdatedField == null)
        throw new Exception("Cant' find custom order field called StockUpdated (type CheckBox / Boolean).");

      stockHasBeenUpdatedField.Value = true;
      order.Save();
    }
  }
}


2. Payment Gateway Callback
var stockHasBeenUpdatedField = (from OrderFieldValue f in order.OrderFieldValues
                               where f.OrderField.SystemName == "StockUpdated"
                               select f).SingleOrDefault();

if (stockHasBeenUpdatedField == null)
 throw new Exception("Cant' find custom order field called StockUpdated (type CheckBox / Boolean).");

if (stockHasBeenUpdatedField.Value == null || Convert.ToBoolean(stockHasBeenUpdatedField.Value) != true)
{
  order.Gateway.SetOrderComplete(order, item.pspReference, order.GatewayResult);
}


Thanks again,

Imar
 

 

You must be logged in to post in the forum