.NET API: GetOrCreateOrderLine() does not add an order line..?
Hi all,
I'm trying to add an order line to an order programmatically using the .NET API.
I have created an event handler and subscribed to the OrderLineAdded event, which works peachy. However when I attempt to add an order line to the order, nothing really happens. It doesn't fail, crash or throw exceptions... the order line simply just does not appear in the order.
You need to add the orderline to the order yourself like this:
if ( orderLine.Id == 0 )
order.AddOrderLine( orderLine );
If you did'nt need to change the unit price you could do it even more simple like I did in one of my resent Tea Commerce projects:
Order order = TeaCommerce.Razor.TeaCommerce.GetOrder();
if ( order != null && order.OrderLines.FirstOrDefault( ol => ol.NodeId.Equals( 1115 ) ) == null ) {
Base.AddOrderLine( 1115, 1 );
Base.AddOrderLine( 1118, 1 );
Base.AddOrderLine( 1119, 1 );
}
I check to see if the order allready have a specific product. If not I add the product and two other complementary products, which you get for free whenever you buy product 1115. I just use the Tea Commerce Base which handles everything for me.
It contains a few other minor upgrades as well. Nothing huge. As always you can install it in Umbraco and it will automatically upgrade to the new version.
Minor bug in the current beta version - it doesnt add the orderline before the quantity is higher than 0. New beta version tomorrow for you to play with :)
[On behalf of Kenn, who is having login-issues after the recent Our changes]
Hi Rune,
Thanks – that solved the cart issue. However, it still does not work. When I add a new order line to the order, Tea Commerce throws the “Collection was modified; enumeration method may not execute” exception (see attached image).
Just for the fun of it I have also tried the following code – but with the same result.
That method still does not work in an event handler (mind you, I'm adding a new order line in the WebshopEvents.OrderLineAdded event). First of all the Order.Save() call causes the event to be fired again, making it run in an infinite loop... and even when coding around that issue, the "Collection was modified" exception is still thrown when Tea Commerce attempts to save the order.
Sure.... sorry for being a pest about this, but being able to add order lines to an order inside the WebshopEvents is critical to our choice of commerce product for an upcoming project.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TeaCommerce.Data.Extensibility;
using TeaCommerce.Data;
namespace Illumi.TeaCommerce.Test {
publicclassExtension : ITeaCommerceExtension {
publicvoid Initialize() {
WebshopEvents.OrderLineAdded += WebshopEvents_OrderLineAdded;
}
void WebshopEvents_OrderLineAdded(Order order, OrderLine orderLine) {
var theOrder = global::TeaCommerce.Razor.TeaCommerce.GetOrder();
var newOrderLine = theOrder.GetOrCreateOrderLine(1106, null, false);
if(newOrderLine.UnitPrice != 100) {
newOrderLine.UnitPrice = 100;
theOrder.Save();
}
// the following code (using the Order object passed to the event) does not work either//var newOrderLine = order.GetOrCreateOrderLine(1106, null, false);//if(newOrderLine.UnitPrice != 100) {// newOrderLine.UnitPrice = 100;// order.Save();//}
}
}
}
We will look into it. One thing you can optimize is that you get the order in the event parameter so you dont need to get the order - just a minor thing :)
Thanks for testing the product and your feedback is super - hope to have a solution very soon.
There you are. A new, and yet again debugged, beta package is uploaded.
The bug was easily fixed, but we then desided that the API is very unlogical with the GetOrCreateOrderLine method. We refactored the whole create orderline part and theres now two new, and much more simple methods called "CreateOrderLine" and "CreateUniqueOrderLine". You will then have to check for the existense of the order line yourselves.
I can't log in with the account that created this thread, so I guess I won't be able to close it... but if I ever do receive a new password for that account, I'll make sure to do so. In any case, consider this solved and closed :-)
.NET API: GetOrCreateOrderLine() does not add an order line..?
Hi all,
I'm trying to add an order line to an order programmatically using the .NET API.
I have created an event handler and subscribed to the OrderLineAdded event, which works peachy. However when I attempt to add an order line to the order, nothing really happens. It doesn't fail, crash or throw exceptions... the order line simply just does not appear in the order.
This is basically the code I'm using:
I have tried a bunch of different combinations of parameters to GetOrCreateOrderLine(), but nothing is working.
Any ideas?
Hi Kenn,
You need to add the orderline to the order yourself like this:
if ( orderLine.Id == 0 ) order.AddOrderLine( orderLine );
If you did'nt need to change the unit price you could do it even more simple like I did in one of my resent Tea Commerce projects:
I check to see if the order allready have a specific product. If not I add the product and two other complementary products, which you get for free whenever you buy product 1115. I just use the Tea Commerce Base which handles everything for me.
/Rune
Thanks for the tip, Rune. However, we're running Tea Commerce 1.4, and Order does not appear to contain a method called AddOrderLine?
(I'm working with Kenn on this project, so this post is on behalf of him)
Hi Dan and Kenn,
Yes, you are absolutely right. That method is internal.
I have now changed a few lines of code and the GetOrCreateOrderLine now actually adds the orderline to the order when you call it.
I have uploaded a beta package of Tea Commerce 1.4.1.0 here here: http://our.umbraco.org/projects/website-utilities/tea-commerce
It contains a few other minor upgrades as well. Nothing huge. As always you can install it in Umbraco and it will automatically upgrade to the new version.
/Rune
Awesome — thanks for the quick turnaround!
No problem, happy to help. Please test if it works and mark the correct answer, for others to see.
/Rune
Minor bug in the current beta version - it doesnt add the orderline before the quantity is higher than 0. New beta version tomorrow for you to play with :)
Hi guys,
I have uploaded a new beta fixing this problem.
/Rune
Thanks — we'll update to the new version asap!
[On behalf of Kenn, who is having login-issues after the recent Our changes]
Hi Rune,
Thanks – that solved the cart issue. However, it still does not work. When I add a new order line to the order, Tea Commerce throws the “Collection was modified; enumeration method may not execute” exception (see attached image).
Just for the fun of it I have also tried the following code – but with the same result.
The order line is added to the order, but when Tea Commerce tries saving the order, it throws the exception.
Hi Dan,
Matt have found the correct way to do it in this post:
http://our.umbraco.org/projects/website-utilities/tea-commerce/tea-commerce-support/21953-Creating-an-order-and-adding-product-lines-Net-API-Getting-started?p=1
Your way, we have desided here at HQ, is the wrong way :)
/Rune
Ah, well there you go. We'll give that a try!
(gee.... I had to create a new profile, sigh)
Hi Rune,
That method still does not work in an event handler (mind you, I'm adding a new order line in the WebshopEvents.OrderLineAdded event). First of all the Order.Save() call causes the event to be fired again, making it run in an infinite loop... and even when coding around that issue, the "Collection was modified" exception is still thrown when Tea Commerce attempts to save the order.
/Kenn
Please post your event code and let me have a look at it. Sounds weird, and I can debug it.
/Rune
Sure.... sorry for being a pest about this, but being able to add order lines to an order inside the WebshopEvents is critical to our choice of commerce product for an upcoming project.
Hi Kenn
We will look into it. One thing you can optimize is that you get the order in the event parameter so you dont need to get the order - just a minor thing :)
Thanks for testing the product and your feedback is super - hope to have a solution very soon.
Hi Guys,
I have found the problem in our event handler and I'm fixing it now. Will post you a revised version of your own code as well.
/Rune
There you are. A new, and yet again debugged, beta package is uploaded.
The bug was easily fixed, but we then desided that the API is very unlogical with the GetOrCreateOrderLine method. We refactored the whole create orderline part and theres now two new, and much more simple methods called "CreateOrderLine" and "CreateUniqueOrderLine". You will then have to check for the existense of the order line yourselves.
And heres your code back in a testet version:
Thanks for your fantastic feedback. It really helps us make our product better (And less faulty)
/Rune
Cheers guys, that works. Thanks.
I can't log in with the account that created this thread, so I guess I won't be able to close it... but if I ever do receive a new password for that account, I'll make sure to do so. In any case, consider this solved and closed :-)
~Kenn
Thats good to hear. Feel free to write again if you have more feedback or problems.
/Rune
is working on a reply...