Developer forum

Forum » Development » Free products

Reply

Hi

We have made a function that allows customers to add free products to the shopping cart.

The Sales Discount module allows to add a free product to selected products. We are not able to use that function because our customer wants to let their customers choose witch variant they want to add. For example: A customer bought a red XL size product, won't be happy with a free blue S size product.

The code we made to add free products works:

Protected Sub AddDiscount(ByVal sender As ObjectByVal e As CommandEventArgs)
        Dim myOrder As eCommerce.Orders.Order = DirectCast(Session("EcomCart"), eCommerce.Orders.Order)

        Dim myOl As eCommerce.Orders.OrderLine = eCommerce.Orders.OrderLine.Create(e.CommandArgument.ToString)
        Dim btnToevoegen As Button = CType(sender, Button)

        Dim ddlVarianten As DropDownList = CType(btnToevoegen.Parent.FindControl("ddlVarianten"), DropDownList)
        Dim sVariantID As String = ddlVarianten.SelectedItem.Value
        Dim sVariantName As String = ddlVarianten.SelectedItem.Text

        Dim myFreeProduct As eCommerce.Products.Product = eCommerce.Products.Product.Create(e.CommandName, sVariantID)

        Dim ol As New Dynamicweb.eCommerce.Orders.OrderLine(myFreeProduct)
        ol.Order = myOrder
        ol.Quantity = myOl.Quantity
       ol.ProductName = myFreeProduct.Name & " * " & GetTranslation("Gratis", sCulture) & " *"
        ol.ProductVariantID = myFreeProduct.VariantID
        ol.ProductVariantText = sVariantName
        ol.SetUnitPrice(Convert.ToDouble(0))
        ol.Reference = myOl.ID.ToString
        ol.Type = Dynamicweb.Base.ChkNumber(Dynamicweb.eCommerce.Orders.OrderLine.OrderLineType.Fixed).ToString()
        ol.DiscountID = sDiscountID
        myOrder.OrderLines.Add(ol, False)
        myOrder.Save()

        Response.Redirect(Request.RawUrl)
    End Sub

The only problem is when the shop also has a salesdiscount like a discount with percentage.
That discount also invokes the free products in the cart of the customer, but calculates the discount on the original productprice.

So the customer gets a free product, andalso extra discount.

I've tried to add the free product on different ways, in stead of the orderlinetype 'fixed', with the type 'discount' and 'productdiscount'. With that types I can see that the free product is added to the database, but with a pageload it's already removed from the cart. (Probably because the properties don't match with the discountproperties).

Is there a way to tell a Sales Discount not to add the discount on an orderline with orderlinetype 2 (fixed)? 
Or is there an other solution to add free products, witch let's customers choose there variant?


Replies

 
Reply
Hi Martijn!
 I think the most appropriate solution would be to do so:
1. Make a custom orderfield to save the selected free product
2. Write SalesDiscountProvider descendant, which will create a discount line based by the value in this custom field

Kind regards,
Vladimir





 
Reply
Hi Vladimir

Thanks for your quick response.
It is unfortunately no solution for our situation.

We need to dynamically add free products. For example, if there are four products in the cart (which are characterized as a product with free product), the customer needs to have the possibilty to add 4 products to the cart for free.

Gr.
Martijn
 
Reply
Hi Martijn!
Ok, In that case, let's just create custom field for order line.
So your AddDiscount function will look like:

Protected Sub AddDiscount(ByVal sender As ObjectByVal e As CommandEventArgs)
        Dim myOrder As eCommerce.Orders.Order = DirectCast(Session("EcomCart"), eCommerce.Orders.Order)

        Dim myOl As eCommerce.Orders.OrderLine = eCommerce.Orders.OrderLine.Create(e.CommandArgument.ToString)
        Dim btnToevoegen As Button = CType(sender, Button)

        Dim ddlVarianten As DropDownList = CType(btnToevoegen.Parent.FindControl("ddlVarianten"), DropDownList)
        Dim sVariantID As String = ddlVarianten.SelectedItem.Value
        Dim sVariantName As String = ddlVarianten.SelectedItem.Text

       myOl .OrderLineFieldValue(“MyFreeProductField”).Value = String.Format(“{0}-{1}-{2}”, e.CommandName, sVariantID, sVariantName )
        myOl .Save()

        Response.Redirect(Request.RawUrl)
    End Sub

And  step 2 - a new SalesDiscountProvider should now for each order line with filled "MyFreeProductField" create an discount line of type "ProductDiscount" with this product


Best regards, Vladimir
 
Reply
Hi Vladimir

Thanks for your support and advise.
I will try to make it work as you have described.

Gr
Martijn

 

You must be logged in to post in the forum