I need to check if the basket has got anything in and then clear all the items from it in c#. Has anyone done this or could give me some pointers along the way.
I was doing this exact thing today - if you try and loop through and delet the rows it throws an error. I got round it by storing all row ids in an array then deleting them using the array.
Hope that helps! :)
K
//removes all current orderlines from basket //--------------------------------------------------------
ArrayList orderLines = new ArrayList(); foreach (var orderline in purchaseOrder.OrderLines) { orderLines.Add(orderline.Id); }
foreach (var x in orderLines) { purchaseOrder.RemoveOrderLine(OrderLine.Get(x)); } //--------------------------------------------------------
Thanks i didn't know that and it looks like it hasn't worked as well as i'd hoped. People are still able to add the same product again, my 'shop' is only meant to have 1 item that can be added only once.
As for uCommerce, i like it for it's configurability but that's, in my opinion, it's biggest weakness. Maybe i'm expecting too much from it but sometimes i just want a shop that i can change the skin on and i don't have to build a nice basket front end etc.... We have used uCommerce a few times bit we are now looking at Tea Commerce as products are listed as nodes in Content and there is a bit more pre-built.
There were a couple of things with the code you posted which is why i didn't use it which is what i posted above. Can you think of why it wouldn't like the lines i posted above?
I wanted to do a separate response to address the complexity of uCommerce. I fully agree that simple store scenarios take too much effort to get up and running and that's why out next project is the uCommerce Razor Store, which is a complete set of templates and macros wrapped up in a nice site that is ready to go out of the box.
This will make it significantly easier to get core e-commerce scenarios up and running and customizing it will be made simpler as well. As a bonus we have a surprise up our sleeve for this release that I think is going to please you :)
Internal development is scheduled to start mid July and we're adding external resources to the development effort shortly after that to wrap up development as rapidly as possible.
This is the absolute top priority in the dev queue at the moment.
'System.Linq.IQueryable<UCommerce.Entities.OrderLine>' does not contain a definition for 'Clear' accepting a first arguement type of 'System.Linq.IQueryable<UCommerce.Entities.OrderLine>' could be found (are you missing a directive or an assembly reference?)
Im looking forward to the razor shop too. It will be a massive help.
However - I doubt I would have learnt as much if i hadn't embarked on recreatng my entire store using razor instead of the supplied xsl. There was quite a learnign curve and im still getting to grips with it - mainly with linq and the API. I also believe a simplified razor friendly API is also on the horizon (Soren will need to confirm that).
On the flip side I would have got the store up and running a lot quicker (still tweaking and still got checkout to implement). It will be very nice to have something out of the box that you can just tweak.
Can't think what the surprise is unless its something to do with a template library (unless its the new API).
Don't want to hijack the thread anyway - just wanted to add a comment as i was subscribed after trying to help before.
@Damian: I can confirm that the Razor Store will indeed feature a new API specifically tuned for use with Razor. It's inspired by the XSLT API which is in place today and will be key in making core e-commerce scenarios much faster to implement.
Just to put the answer as to why my code wasn't working properly was that the project had an old version of the Umbraco.dll in, once i'd updated that it all worked well. Thanks for the help Soren,
As for the surprise, i wonder if the products will be moving into content. That's my suggestion...
Clear the basket
Hi,
I need to check if the basket has got anything in and then clear all the items from it in c#. Has anyone done this or could give me some pointers along the way.
Thanks
Chris
Hi,
Would this code have the desired effect?
var basket = SiteContext.Current.OrderContext.GetBasket().PurchaseOrder;
if (basket.IsBasket)
{
basket.Delete();
}
Thanks
Chris
Hi Chris,
I was doing this exact thing today - if you try and loop through and delet the rows it throws an error. I got round it by storing all row ids in an array then deleting them using the array.
Hope that helps! :)
K
//removes all current orderlines from basket
//--------------------------------------------------------
ArrayList orderLines = new ArrayList();
foreach (var orderline in purchaseOrder.OrderLines)
{
orderLines.Add(orderline.Id);
}
foreach (var x in orderLines)
{
purchaseOrder.RemoveOrderLine(OrderLine.Get(x));
}
//--------------------------------------------------------
Hi Kenny,
Thanks for your reply, can you tell me what the purchaseOrder variable is please? I have the following but it doesn't like it:
It is a PurchaseOrder.
Check you have referenced the namespace:
@using UCommerce.EntitiesV2;
Thanks Damien,
I have that reference but it doesn't seem to like the following bits:
It doesn't like the .Id part and i can't find it if i look through the methods or properties.
It also can't seem to find:
it can't find either the RemoveOrderLine or the Get(x) part of it.
Sorry to be a pain but i need to get this sorted and am struggling.
Thank you for your help once again.
Chris
Could i just do the following:
var purchaseOrder = SiteContext.Current.OrderContext.GetBasket().PurchaseOrder;
foreach (var orderline in purchaseOrder.OrderLines)
{
orderline.Delete();
}
Would that work?
Thanks
Chris
The first part.. Add an order line - you need to add an object to the collection not an id:
PurchaseOrder order = new PurchaseOrder();
OrderLine item = new OrderLine();
// popuplate your order line etc
order.OrderLines.Add(item); // add the item not item.Id
As for clearing it out - what about this?
order.OrderLines.Clear();
order.Save();
Are you persisting the changes with save?
Ive not acutally done anything with the basket yer - not got that far but thats how id expect it to work.
Let me know if it works.
ok, turns out there is a method for doing this...
According to the uCommerce docs, it clears everything out of your basket and removes it.
http://www.ucommerce.dk/docs/html/M_UCommerce_Runtime_OrderContext_ClearBasketInformation.htm
Thanks for your help Damian and Kenny.
Chris
No problems!
Doing the ClearBasketInformation leaves you wih redundant baskets in the databse though - which is why i went for the function above -
Many ways to skin some cats! :)
How are you finding uCommerce?
K
Kenny,
Thanks i didn't know that and it looks like it hasn't worked as well as i'd hoped. People are still able to add the same product again, my 'shop' is only meant to have 1 item that can be added only once.
As for uCommerce, i like it for it's configurability but that's, in my opinion, it's biggest weakness. Maybe i'm expecting too much from it but sometimes i just want a shop that i can change the skin on and i don't have to build a nice basket front end etc.... We have used uCommerce a few times bit we are now looking at Tea Commerce as products are listed as nodes in Content and there is a bit more pre-built.
There were a couple of things with the code you posted which is why i didn't use it which is what i posted above. Can you think of why it wouldn't like the lines i posted above?
Thanks
C
Hi Chris,
If you want to clear items without removing the basket outright you can do the following:
Thanks Soren,
What difference does not removing the basket make? Any?
Chris
I wanted to do a separate response to address the complexity of uCommerce. I fully agree that simple store scenarios take too much effort to get up and running and that's why out next project is the uCommerce Razor Store, which is a complete set of templates and macros wrapped up in a nice site that is ready to go out of the box.
This will make it significantly easier to get core e-commerce scenarios up and running and customizing it will be made simpler as well. As a bonus we have a surprise up our sleeve for this release that I think is going to please you :)
Internal development is scheduled to start mid July and we're adding external resources to the development effort shortly after that to wrap up development as rapidly as possible.
This is the absolute top priority in the dev queue at the moment.
Hi Chris,
The only difference is that you won't have a bunch of abandonned baskets sticking around. This will reuse the existing one when adding new items.
Soren,
Thanks for the response about the shop, we look forward to this razor shop with much anticipation and especially the 'surprise'!
Thanks for the response about the basket, i'll give that a try and see what happens.
Cheers
Chris
Soren,
Put that code in and Visual Studio says
Any ideas as to why this would be?
Thanks
Chris
Which version of uCommerce are you on? I'm guessing it's an older 1.x?
The code I posted will only work for 2.x.
For 1.x the way to get rid of all order lines works like this:
Im looking forward to the razor shop too. It will be a massive help.
However - I doubt I would have learnt as much if i hadn't embarked on recreatng my entire store using razor instead of the supplied xsl. There was quite a learnign curve and im still getting to grips with it - mainly with linq and the API. I also believe a simplified razor friendly API is also on the horizon (Soren will need to confirm that).
On the flip side I would have got the store up and running a lot quicker (still tweaking and still got checkout to implement). It will be very nice to have something out of the box that you can just tweak.
Can't think what the surprise is unless its something to do with a template library (unless its the new API).
Don't want to hijack the thread anyway - just wanted to add a comment as i was subscribed after trying to help before.
Damian
@Damian: I can confirm that the Razor Store will indeed feature a new API specifically tuned for use with Razor. It's inspired by the XSLT API which is in place today and will be key in making core e-commerce scenarios much faster to implement.
Awesome :)
Just to put the answer as to why my code wasn't working properly was that the project had an old version of the Umbraco.dll in, once i'd updated that it all worked well. Thanks for the help Soren,
As for the surprise, i wonder if the products will be moving into content. That's my suggestion...
Thanks guys.
is working on a reply...