Copied to clipboard

Flag this post as spam?

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


  • Tor Langlo 189 posts 532 karma points
    Feb 28, 2023 @ 19:48
    Tor Langlo
    0

    Get and modify the last added product/order line

    Hi!

    I'm adding a product using the AddProduct(...) method. Now I need to immediately modify the just added product/cart/order line. How do I get a hold of that order line? Background, this is part of a migration from TeaCommerce and I need update the Sku after the product has been added.

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Mar 01, 2023 @ 09:15
    Matt Brailsford
    0

    Hey Tor,

    You could use the OrderProductAddingNotification event that fires every time an item is being added to the order or maybe the OrderLineAddingNotification event which fires when a unique order line is being added to the order.

    From there you can update the order accordingly.

  • Tor Langlo 189 posts 532 karma points
    Mar 02, 2023 @ 18:41
    Tor Langlo
    0

    In this case it turned out I just needed to get the OrderLine.Id for use in an sql update. Since in most cases there are only 1 or 2 lines per order in our system I used another property to identify the correct line (a product uniqueness property).

    In general I'd think it would make sense to return the order line as the result of the AddProduct method. I understand that would be at the expense of the "fluidity" of the method.

  • Asbjørn 82 posts 195 karma points c-trib
    Apr 14, 2023 @ 07:17
    Asbjørn
    0

    I have the same problem. I need to get the newly added orderline to process it further. Doing it in a notification is not an option, since I need to use external data to set various properties, which I won't have access to in the notification.

    For me it makes no sense that the AddProduct method doesn't return some kind of reference to the orderline it just created (either the whole line or just the ID), maybe using an out parameter to keep it fluid. I can't be the only one to need to access the orderline after adding it.

    I suppose I can get the last orderline from the OrderLines property, but that seems a bit dirty...

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Apr 14, 2023 @ 07:26
    Matt Brailsford
    0

    Hey,

    Can someone explain a specific use case as I'm unclear what you are trying to achieve and why it can't all be done via the AddProduct method. If I know more I can try and provide more help.

  • Asbjørn 82 posts 195 karma points c-trib
    Apr 14, 2023 @ 08:10
    Asbjørn
    0

    We are migrating a system from TeaCommerce, so that does naturally affect how the code is structured. We have a booking system that takes a relatively generic Umbraco product (same product is used for every booking) and then we use a lot of both order properties and order line properties to store all the unique details. This information comes from the data the user enters (and is then passed to the backend via an API Controller).

    Today, we have a method that takes the basic information common to all bookings and creates an orderline for this booking. This orderline is then passed onto various specialized methods depending on exactly which type of booking we are working with. These methods will then set additional properties.

    Doing this with Vendr is currently difficult, as I have no way to get the orderline which I just added.

    Now, this could probably be restructured to pre-create the property dictionary and then create the orderline. I suppose there is a performance benefit to pre-creating the property dictionary and allowing Vendr to save all properties at once... Is that why Vendr has this design?

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Apr 14, 2023 @ 11:54
    Matt Brailsford
    0

    Hey Asbjorn,

    Yea, that was going to be the exact solution I would recommend in your case, creating a dictionary and passing it to your processes to populate and then create the order line at the end.

    And yea, in TC there were sooooooo many save operations being triggered which is a) inefficient and b) non-transactional so it would be very easy to get your order into a mixed up state if one element went wrong. By making it all part of a single save operation this should solve both of those issues.

  • Asbjørn 82 posts 195 karma points c-trib
    Apr 14, 2023 @ 12:05
    Asbjørn
    0

    Thank you, Matt. Your support is amazing!

    I guess I just need to unlearn a few TeaCommerce habits ;)

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Apr 14, 2023 @ 13:16
    Matt Brailsford
    0

    No problem at all. Glad I could help.

  • Asbjørn 82 posts 195 karma points c-trib
    Apr 18, 2023 @ 08:05
    Asbjørn
    0

    HI,

    Sorry for bringing this up again, but I hit a case where the suggested workaround won't work.

    So, we're in an API Controller that gets a request to add a line to the order (again, using a custom model posted from the frontend). I create a dictionary with properties and then call AddProduct, no issue there.

    However, after having added the product, I now need to return the result back to the client. This happens with another custom model, which represents just the order line that was just added. The frontend will separately request an updated price, so there is no need to return a lot of data that will not be needed.

    But since I have no reference to the order line after creating it, I have no way to create the custom model from this line. Can I assume that the last orderline in the OrderLines Collection is the one just added?

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Apr 18, 2023 @ 08:15
    Matt Brailsford
    0

    I guess the issue is AddProduct doesn't always result in a new line being added. It could be updating the quantity of an existing orderline if the product already appears in the cart.

    I'll have to review whether adding an out param would be beneficial here, but in the meantime I think your simplest option would be to reload all the items in the cart.

  • Asbjørn 82 posts 195 karma points c-trib
    Apr 18, 2023 @ 08:18
    Asbjørn
    0

    Yes, that is of course a good point. But I do think that having the ability to get some kind of reference to the line being affected (either a new line or the line being updated) would be a useful addition to the API.

    Other than this, I'm enjoying the many improvements in Vendr compared to TeaCommerce :)

  • Tor Langlo 189 posts 532 karma points
    24 days ago
    Tor Langlo
    0

    Just a reminder that to get a hold of the relevant order line after adding a product would still be a useful feature.

    Right now I have a need to capture the relevant data for later transmission to Google Tag Manager (GTM)/Google Analytics. The process starts in the form controller where the product is added to the cart. I need to capture sku, item name, quantity, price, and discount, serialize the data, and save it into a TempData item. After redirecting to and reloading the product page, the data is deserialized from TempData and an eCommerce Json object pushed onto the GTM dataLayer.

Please Sign in or register to post replies

Write your reply to:

Draft