Copied to clipboard

Flag this post as spam?

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


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Chris Knowles 141 posts 222 karma points
    Jun 13, 2012 @ 14:08
    Chris Knowles
    0

    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

  • Chris Knowles 141 posts 222 karma points
    Jun 14, 2012 @ 18:21
    Chris Knowles
    0

    Hi,

    Would this code have the desired effect?

     

                    var basket = SiteContext.Current.OrderContext.GetBasket().PurchaseOrder;

                    if (basket.IsBasket)

                    {

                        basket.Delete();

                    }

     

    Thanks

    Chris

  • Kenny Burns 173 posts 305 karma points
    Jun 15, 2012 @ 09:56
    Kenny Burns
    0

    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));
     }
     //--------------------------------------------------------

  • Chris Knowles 141 posts 222 karma points
    Jun 15, 2012 @ 10:36
    Chris Knowles
    0

    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:

                    var purchaseOrder = SiteContext.Current.OrderContext.GetBasket().PurchaseOrder;
    Thanks for your help.
    Chris
  • Damian Green 452 posts 1433 karma points
    Jun 15, 2012 @ 10:45
    Damian Green
    0

    It is a PurchaseOrder.

    Check you have referenced the namespace:

    @using UCommerce.EntitiesV2;

     

  • Chris Knowles 141 posts 222 karma points
    Jun 15, 2012 @ 10:49
    Chris Knowles
    0

    Thanks Damien,

    I have that reference but it doesn't seem to like the following bits:

    orderLines.Add(orderline.Id) 

    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:

    purchaseOrder.RemoveOrderLine(OrderLine.Get(x))

    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

  • Chris Knowles 141 posts 222 karma points
    Jun 15, 2012 @ 11:23
    Chris Knowles
    0

    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

  • Damian Green 452 posts 1433 karma points
    Jun 15, 2012 @ 11:42
    Damian Green
    0

    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

     

  • Damian Green 452 posts 1433 karma points
    Jun 15, 2012 @ 11:44
    Damian Green
    0

    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.

  • Chris Knowles 141 posts 222 karma points
    Jun 15, 2012 @ 12:12
    Chris Knowles
    0

    ok, turns out there is a method for doing this... 

    SiteContext.Current.OrderContext.ClearBasketInformation();

    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

  • Kenny Burns 173 posts 305 karma points
    Jun 15, 2012 @ 16:37
    Kenny Burns
    0

    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

  • Chris Knowles 141 posts 222 karma points
    Jul 02, 2012 @ 16:38
    Chris Knowles
    0

    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

  • Søren Spelling Lund 1797 posts 2786 karma points
    Jul 03, 2012 @ 10:05
    Søren Spelling Lund
    0

    Hi Chris,

    If you want to clear items without removing the basket outright you can do the following:

      var order = SiteContext.Current.OrderContext.GetBasket().PurchaseOrder;
      order.OrderLines.Clear();
      order.Save();
  • Chris Knowles 141 posts 222 karma points
    Jul 03, 2012 @ 10:11
    Chris Knowles
    0

    Thanks Soren,

    What difference does not removing the basket make? Any?

    Chris

  • Søren Spelling Lund 1797 posts 2786 karma points
    Jul 03, 2012 @ 10:15
    Søren Spelling Lund
    0

    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.

  • Søren Spelling Lund 1797 posts 2786 karma points
    Jul 03, 2012 @ 10:16
    Søren Spelling Lund
    0

    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.

  • Chris Knowles 141 posts 222 karma points
    Jul 03, 2012 @ 10:20
    Chris Knowles
    0

    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

  • Chris Knowles 141 posts 222 karma points
    Jul 03, 2012 @ 10:32
    Chris Knowles
    0

    Soren,

    Put that code in and Visual Studio says 

    '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?)

     

    Any ideas as to why this would be?

    Thanks

    Chris

  • Søren Spelling Lund 1797 posts 2786 karma points
    Jul 03, 2012 @ 10:47
    Søren Spelling Lund
    0

    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:

    OrderLine.Delete(x => x.OrderId == order.OrderId);
  • Damian Green 452 posts 1433 karma points
    Jul 03, 2012 @ 11:12
    Damian Green
    0

    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

     

  • Søren Spelling Lund 1797 posts 2786 karma points
    Jul 03, 2012 @ 11:42
    Søren Spelling Lund
    0

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

  • Damian Green 452 posts 1433 karma points
    Jul 03, 2012 @ 11:57
    Damian Green
    0

    Awesome :)

  • Chris Knowles 141 posts 222 karma points
    Jul 03, 2012 @ 12:00
    Chris Knowles
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft