Posted on 10/09/2014 10:09:45
Hi Marco
You can call the ordline builder in Ecommerce:
Dynamicweb.eCommerce.Frontend.Cart.CartCatch.OrderLineBuilder(Dynamicweb.eCommerce.Common.Context.Cart, ...)
And here is the code inside that method. It has a lot of things you do not need, but you can see how it should be done.
Dim QuantityOrderLine As Double = Quantity
If QuantityCheck Then
If QuantityOrderLine <= 0 Then
Return Nothing
End If
Else
If QuantityOrderLine <= 0 Then
QuantityOrderLine = 1
End If
End If
Dim ol As New OrderLine
ol.ID = ""
ol.OrderID = EcomCart.ID
ol.ProductID = ProductID
ol.ProductVariantID = VariantID
ol.Quantity = QuantityOrderLine
ol.Modified = Now
ol.ProductVariantText = VariantText
ol.UnitID = UnitID
ol.Type = Type
ol.ListID = listId
'TFC - Removed for security reasons
'If Base.Request("UnitPrice") <> "" Then
' ol.SetUnitPrice(Base.DoubleFromString(Base.Request("UnitPrice")))
'End If
If Base.Request("DontSaveReference") = "" And Not RefUrl Is Nothing Then
If RefUrl.Length > 0 Then
If RefUrl.Length <= 255 Then
ol.Reference = RefUrl
Else
ol.Reference = RefUrl.Substring(0, 255)
End If
End If
End If
ol.PageID = pageId
EcomCart.IsCart = True
ol.Order = EcomCart
'### BOM being added to order, this is tricky, don't mess it up please.
'Dim ProductToPutInCart As New Product(OrderLine.ProductID, OrderLine.ProductVariantID)
Dim ProductToPutInCart As Product = Product.GetProductByID(ol.ProductID, ol.ProductVariantID)
If ProductToPutInCart Is Nothing Then
ProductToPutInCart = New Product()
End If
If ol.HasType(OrderLine.OrderLineType.PointProduct) AndAlso (ProductToPutInCart.DefaultPoints <= 0 OrElse (Not IsNothing(Modules.UserManagement.User.Current(Modules.UserManagement.PagePermissionLevels.Frontend)) AndAlso (ProductToPutInCart.DefaultPoints * Quantity + Base.ChkDouble(EcomCart.TotalPoints)) > Dynamicweb.Modules.UserManagement.User.Current(Modules.UserManagement.PagePermissionLevels.Frontend).PointBalance)) Then
Return Nothing
End If
' Check stock and reserve
If ProductReserve.Enabled AndAlso ProductToPutInCart.Type <> ProductType.Service AndAlso ol.HasType({OrderLine.OrderLineType.Product, OrderLine.OrderLineType.PointProduct}) Then
Dim cartStock As Double = ProductReserve.GetReservedAmount(ol.ProductID, ol.ProductVariantID) + QuantityOrderLine
If ProductReserve.GetReserveMode = ProductReserve.ReserveMode.modeCheckout Then
' When products is reserved only in checkout step we should take into account is there such product exist in cart
cartStock += EcomCart.OrderLines.Where(Function(cartLine) cartLine.ProductID = ProductID AndAlso cartLine.ProductVariantID = VariantID).Select(Function(cartLine) cartLine.Quantity).FirstOrDefault()
End If
If ProductToPutInCart.UnitStock < cartStock Then
Return Nothing
End If
End If
If ProductToPutInCart.VariantID = "" AndAlso ol.ProductVariantID <> "" Then
ProductToPutInCart.VirtualVariantID = ol.ProductVariantID
End If
'#################
'Resolve VariantText
'#################
If ol.ProductVariantID <> "" And ol.ProductVariantText = "" Then
'TFC - 17/04-2007
ol.ProductVariantText = eCommerce.Variants.VariantNumber.VariantName(ol.ProductVariantID, Context.LanguageID)
End If
'#################################
'Resolve Product information
'---------------------------------
'ProductName
'ProductNumber
'#################################
If ProductToPutInCart.ID <> "" Then
If ProductName = "" Then
ol.ProductName = ProductToPutInCart.Name
Else
ol.ProductName = ProductName
End If
If ProductNumber = "" Then
ol.ProductNumber = ProductToPutInCart.Number
Else
ol.ProductNumber = ProductNumber
End If
End If
'OrderLineFieldValues
If orderLineFieldValues Is Nothing Then
orderLineFieldValues = New OrderLineFieldValueCollection()
End If
ol.OrderLineFieldValues = orderLineFieldValues
If ProductToPutInCart.Type = ProductType.BOM Then
For Each Item As ProductItem In ProductToPutInCart.Items
Dim bomProduct As Product
Dim bomQuantity As Double
Dim bomItemID As String = Nothing
If Item.BomGroupID <> String.Empty Then
bomItemID = Item.ID
bomQuantity = 1
If Base.Request(Item.ID) <> "" Then
If Base.Request(Item.ID).Contains(",") Then
For Each bomID As String In Base.Request(Item.ID).Split(","c)
bomProduct = Item.Products.getProductById(bomID)
ol.BOMOrderLines.Add(BomOrderlineBuilder(bomProduct, EcomCart, bomQuantity, ol.ID, RefUrl, bomItemID), False) '***NEW
Next
Else
bomProduct = Item.Products.getProductById(Base.Request(Item.ID))
ol.BOMOrderLines.Add(BomOrderlineBuilder(bomProduct, EcomCart, bomQuantity, ol.ID, RefUrl, bomItemID), False) '***NEW
End If
Else
bomProduct = Item.Products.getProductById(Item.DefaultProductID)
ol.BOMOrderLines.Add(BomOrderlineBuilder(bomProduct, EcomCart, bomQuantity, ol.ID, RefUrl, bomItemID), False) '***NEW
End If
Else
bomProduct = Item.Products.getProductById(Item.DefaultProductID)
bomQuantity = Item.Quantity
ol.BOMOrderLines.Add(BomOrderlineBuilder(bomProduct, EcomCart, bomQuantity, ol.ID, RefUrl, bomItemID), False) '***NEW
End If
Next
End If
ol.Product = ProductToPutInCart
If AddToCart Then
EcomCart.OrderLines.Add(ol, True)
End If
Return ol