Hi,
We have an upcomming site with 2 stocklocations and 2 units, when the customer adds a product to the cart he selects what unit he want to buy.
What stocklocation to use is defined on the customer (standard).
It all works until you make a few orders and notice that stocks are not correctly updated, its the correct stocklocation but not the correct unit that has been updated.
Upon inspecteing the code, the issue seems to lie in the StockLocationManager, line 54 (marked with red):
Dim productUnits As IEnumerable(Of StockUnit) = StockUnit.GetProductStockUnits(orderLine.ProductId, orderLine.ProductVariantId)
If Not productUnits.Any() AndAlso Not String.IsNullOrEmpty(orderLine.ProductVariantId) AndAlso String.IsNullOrEmpty(orderLine.Product.VariantId) Then
' if there are no stock units for simple variant, take them from main product
productUnits = StockUnit.GetProductStockUnits(orderLine.Product)
End If
Dim productUnitsByLocation As IEnumerable(Of StockUnit) = productUnits.
Where(Function(stockUnit)
Return (IsNothing(Common.Context.StockLocation) OrElse stockUnit.StockLocationId = Common.Context.StockLocation.ID)
End Function).
OrderBy(Function(stockUnit)
Dim location As StockLocation = Nothing
If stockUnit.StockLocationId <> 0 Then
location = StockLocation.GetStockLocationByIdAndLanguage(stockUnit.StockLocationId, orderLine.Product.LanguageId)
End If
If IsNothing(location) Then
Return 999
Else
stockUnit.StockLocationId = location.ID
Return location.SortOrder
End If
End Function)
If Not IsNothing(Common.Context.StockLocation) AndAlso productUnitsByLocation.Any() Then
unitId = productUnitsByLocation.First().UnitId
Else
unitId = If(String.IsNullOrEmpty(orderLine.UnitID), orderLine.Product.DefaultUnitID, orderLine.UnitID)
End If
Are there ever any reason not to attempt to find the correct unit instead of just taking the first? Something like this (Disclaimer, I'm not a VB export):
Dim stockUnitLocation = productUnitsByLocation.FirstOrDefault(Function(loc) String.Equals(loc.UnitId, orderLine.UnitId)) If IsNothing(stockUnitLocation) stockUnitLocation = productUnitsByLocation.First() End If unitId = stockUnitLocation.UnitId
Regards Simon Nordahl