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.
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.
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.
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...
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.
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?
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.
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?
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.
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 :)
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.
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.
Hey Tor,
You could use the
OrderProductAddingNotification
event that fires every time an item is being added to the order or maybe theOrderLineAddingNotification
event which fires when a unique order line is being added to the order.From there you can update the order accordingly.
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.
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...
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.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?
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.
Thank you, Matt. Your support is amazing!
I guess I just need to unlearn a few TeaCommerce habits ;)
No problem at all. Glad I could help.
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?
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.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 :)
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.
is working on a reply...