Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kenn Jacobsen 1 post 21 karma points
    Jul 06, 2011 @ 10:23
    Kenn Jacobsen
    0

    .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:

        public class Extension : ITeaCommerceExtension {
            public void Initialize() {
                WebshopEvents.OrderLineAdded += WebshopEvents_OrderLineAdded;
            }
    
            void WebshopEvents_OrderLineAdded(Order order, OrderLine orderLine) {
                var newOrderLine = order.GetOrCreateOrderLine(1106, nullfalse);
                newOrderLine.UnitPrice = 0;
                order.Save();
            }
        }
    

    I have tried a bunch of different combinations of parameters to GetOrCreateOrderLine(), but nothing is working.

    Any ideas?

  • Rune Grønkjær 1372 posts 3103 karma points
    Jul 06, 2011 @ 10:34
    Rune Grønkjær
    0

    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:

    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.

    /Rune

  • Dan Okkels Brendstrup 101 posts 197 karma points
    Jul 06, 2011 @ 12:34
    Dan Okkels Brendstrup
    0

    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)

  • Rune Grønkjær 1372 posts 3103 karma points
    Jul 06, 2011 @ 12:55
    Rune Grønkjær
    0

    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

  • Dan Okkels Brendstrup 101 posts 197 karma points
    Jul 06, 2011 @ 13:37
    Dan Okkels Brendstrup
    0

    Awesome — thanks for the quick turnaround!

  • Rune Grønkjær 1372 posts 3103 karma points
    Jul 06, 2011 @ 13:38
    Rune Grønkjær
    0

    No problem, happy to help. Please test if it works and mark the correct answer, for others to see.

    /Rune

  • Anders Burla 2560 posts 8256 karma points
    Jul 06, 2011 @ 19:59
    Anders Burla
    0

    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 :)

  • Rune Grønkjær 1372 posts 3103 karma points
    Jul 07, 2011 @ 10:51
    Rune Grønkjær
    0

    Hi guys,

    I have uploaded a new beta fixing this problem.

    /Rune

  • Dan Okkels Brendstrup 101 posts 197 karma points
    Jul 07, 2011 @ 10:57
    Dan Okkels Brendstrup
    0

    Thanks — we'll update to the new version asap!

  • Dan Okkels Brendstrup 101 posts 197 karma points
    Jul 07, 2011 @ 11:46
    Dan Okkels Brendstrup
    0

    [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. 

                     void WebshopEvents_OrderLineAdded(Order order, OrderLine orderLine) {
                              var newOrderLine = order.GetOrderLine(1106, nullfalse);
                              if(newOrderLine == null) {
                                       global::TeaCommerce.Base.AddOrderLine(1106, 1);
                                       newOrderLine = order.GetOrderLine(1106, nullfalse);
                              }
                              newOrderLine.UnitPrice = 100;
                     }

    The order line is added to the order, but when Tea Commerce tries saving the order, it throws the exception.

  • Rune Grønkjær 1372 posts 3103 karma points
    Jul 07, 2011 @ 11:51
    Rune Grønkjær
    0

    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

  • Dan Okkels Brendstrup 101 posts 197 karma points
    Jul 07, 2011 @ 12:01
    Dan Okkels Brendstrup
    0

    Ah, well there you go. We'll give that a try!

  • Kenn Jacobsen 133 posts 791 karma points MVP 4x c-trib
    Jul 07, 2011 @ 12:05
    Kenn Jacobsen
    0

    (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

     

  • Rune Grønkjær 1372 posts 3103 karma points
    Jul 07, 2011 @ 12:21
    Rune Grønkjær
    0

    Please post your event code and let me have a look at it. Sounds weird, and I can debug it.

    /Rune

  • Kenn Jacobsen 133 posts 791 karma points MVP 4x c-trib
    Jul 07, 2011 @ 12:31
    Kenn Jacobsen
    1

    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 {
        public class Extension : ITeaCommerceExtension {
            public void Initialize() {
                WebshopEvents.OrderLineAdded += WebshopEvents_OrderLineAdded;
            }
    
            void WebshopEvents_OrderLineAdded(Order order, OrderLine orderLine) {
                var theOrder = global::TeaCommerce.Razor.TeaCommerce.GetOrder();
                var newOrderLine = theOrder.GetOrCreateOrderLine(1106, nullfalse);
                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();
                //}
            }
        }
    }
    
  • Anders Burla 2560 posts 8256 karma points
    Jul 07, 2011 @ 12:42
    Anders Burla
    0

    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.

  • Rune Grønkjær 1372 posts 3103 karma points
    Jul 07, 2011 @ 13:03
    Rune Grønkjær
    0

    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

  • Rune Grønkjær 1372 posts 3103 karma points
    Jul 07, 2011 @ 14:52
    Rune Grønkjær
    0

    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:

    void WebshopEvents_OrderLineAdded( Order order, OrderLine orderLine ) {
      OrderLine newOrderLine = order.OrderLines.FirstOrDefault( ol => ol.NodeId == 1106 );
    
      if ( newOrderLine == null ) {
        newOrderLine = order.CreateOrderLine( 1106, 1 );
        newOrderLine.UnitPrice = 100;
    
        order.Save();
      }
    }

    Thanks for your fantastic feedback. It really helps us make our product better (And less faulty)

    /Rune

  • Kenn Jacobsen 133 posts 791 karma points MVP 4x c-trib
    Jul 08, 2011 @ 08:14
    Kenn Jacobsen
    1

    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

  • Rune Grønkjær 1372 posts 3103 karma points
    Jul 08, 2011 @ 08:16
    Rune Grønkjær
    0

    Thats good to hear. Feel free to write again if you have more feedback or problems.

    /Rune

Please Sign in or register to post replies

Write your reply to:

Draft