Developer forum

Forum » Development » Creating new orderline in NotificationSubsriber doesn't seem to add orderline

Creating new orderline in NotificationSubsriber doesn't seem to add orderline

Marie Louise Veigert
Reply

Hi,

Im currently trying to add a fixed orderline type to a Order, when the Cart is rendered. It's due to minimumbuy - if the customer doesn't buy for at least 150dkk there should be added a orderline "minimumbuy" for the rest about up to 150dkk.

I am trying to do this, but the orderline doesn't seem to be added. When I debug it looks like it is :) 

Im using [Subscribe(Ecommerce.Cart.Loaded)]

Any thought on what I could be missing?

 var currency = new Currency();
 currency.Code = currentCurrency;

var priceInfo = new PriceInfo(currency);
var addedPrice = minimumBuy - orderPrice;
priceInfo.PriceWithVAT = addedPrice;
                       
var ol = Dynamicweb.Ecommerce.Services.OrderLines.Create(cart, "Mindstekøb", 1, priceInfo, OrderLineType.Fixed, "", "");
cart.OrderLines.Add(ol);
Dynamicweb.Ecommerce.Services.Orders.Save(cart);


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

I think you might need to call Dynamicweb.Ecommerce.Services.OrderLines.Save with the orderline you just add...

Votes for this answer: 1
 
Marie Louise Veigert
Reply

I couldn't get this custom orderline to work properly, so ended up with creating a "minimum price" product, which I changed the price on depending on how much is added to the cart.

Code:

var currency = new Currency();
currency.Code = cart.Currency.Code;

var addedPrice = minimumBuy - orderPrice;

var priceInfo = new PriceInfo(currency);
priceInfo.PriceWithVAT = addedPrice;

var specialProd = Dynamicweb.Ecommerce.Services.Products.GetProductById("PROD303", "", cart.LanguageId, false);
var ol = Dynamicweb.Ecommerce.Services.OrderLines.Create(cart, specialProd, 1, null, priceInfo);
Dynamicweb.Ecommerce.Services.OrderLines.SetUnitPrice(ol, addedPrice, false);
Dynamicweb.Ecommerce.Services.OrderLines.Save(ol);
Dynamicweb.Ecommerce.Services.Orders.Save(cart);

But Im having an issue with the SetUnitPrice. It seems like its only possible to add the price without VAT.

Just as it isn't possible to change the price in the Create method with the PriceInfo object.

I would like to be able to set the price with VAT, because it's the value we have for minimum buy.
Is it possible in another way or do we need to calculate the price without VAT?

 
Marie Louise Veigert
Reply

I found a solution for my issue in comment above. Posting if anyone else needs to do something similair.

 

 if (orderPrice < minimumBuy)
{
     var specialProd = Dynamicweb.Ecommerce.Services.Products.GetProductById("PROD303", "", cart.LanguageId, false);

     var ol = Dynamicweb.Ecommerce.Services.OrderLines.Create(cart, specialProd, 1.0, null, null);
     ol.OrderLineType = OrderLineType.Fixed;
     ol.UnitPrice.PriceWithVAT = minimumBuy - orderPrice;        

     Dynamicweb.Ecommerce.Services.OrderLines.Save(ol);
     Dynamicweb.Ecommerce.Services.Orders.Save(cart);
}

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Thanks for sharing!

 
Marie Louise Veigert
Reply

I have an issue with deleting an orderline. It isn't getting deleted with this code.
I saw this post and can't seem to use the "remove" method in our solution.: https://doc.dynamicweb.com/forum/development/development/removing-orderlines-in-code 
Is this method removed and the issue why it wont be deleted?

My code:

 var orderline = fixedOrderlines.FirstOrDefault();
            var price = orderPrice - orderline.UnitPrice.PriceWithVAT;
            if (orderPrice >= minimumBuy)
            {
                if (price >= minimumBuy)
                {                    
                    Dynamicweb.Ecommerce.Services.OrderLines.Delete(orderline.Id);
                    Dynamicweb.Ecommerce.Services.Orders.Save(order);
                }
                else if (price < minimumBuy)
                {
                    AdjustOrderline(orderline, order, minimumBuy - price);
                }
            }
 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Try using Order.Orderlines.Remove() instead.

Your code will remove the orderline from database - but not from the order object - so when you save the order, the orderline will probably be recreated.

BR Nicoali

 
Marie Louise Veigert
Reply

And this wont remove all the other lines as well?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Order.Orderlines.Remove(orderline) is the exact call - so only the one you pass :-)

Votes for this answer: 1
 
Marie Louise Veigert
Reply

It did the trick! Thank you :)

 

You must be logged in to post in the forum