Page 1 of 1

Copy a basket, but leave items in original basket

Posted: Fri Jun 19, 2009 1:12 pm
by jmestep
Has anyone found a method to copy the items in a basket and leave the items in the old basket also?
Basket.Transfer deletes them from the first basket and in a situation where you are emailing a basket to a friend or friends, it works fine according a post from mazhar earlier, but we need to keep the contents of the original basket for the potentiality of another friend viewing it.

Re: Copy a basket, but leave items in original basket

Posted: Fri Jun 19, 2009 9:33 pm
by AbleMods
Have you tried making a new basket instance and doing Token.Instance.User.Baskets.Add(_NewBasket)?

Re: Copy a basket, but leave items in original basket

Posted: Sat Jun 20, 2009 6:46 am
by jmestep
I'll look at that. I make a new basket, new user, access code in the code to send a basket email, then another new basket as a result of clicking the link in the email and going to the basket page.

Re: Copy a basket, but leave items in original basket

Posted: Sat Jun 20, 2009 7:48 am
by AbleMods
I'm pretty sure baskets is another class just like all the others. You can make a new instance and .add(_new).

The question would be what Able considers "the basket" when the user has multiple baskets. Is it the most recently saved basket? Or is it the first basket?

Re: Copy a basket, but leave items in original basket

Posted: Mon Jun 22, 2009 6:32 am
by mazhar
jmestep wrote:Has anyone found a method to copy the items in a basket and leave the items in the old basket also?
Basket.Transfer deletes them from the first basket and in a situation where you are emailing a basket to a friend or friends, it works fine according a post from mazhar earlier, but we need to keep the contents of the original basket for the potentiality of another friend viewing it.
Try to accomplish it as below, the key point is to call basket.Save before assigning any basket item to it. The reason is basket items are attached to basket depending upon basket it, so in order to put items in to new basket first you need to have a basket with new id.

Code: Select all

Basket basket = new Basket();
        basket.UserId = Token.Instance.UserId;
        basket.Save();
        foreach (BasketItem bi in Token.Instance.User.Basket.Items)
            basket.Items.Add(bi);
        basket.Save();

Re: Copy a basket, but leave items in original basket

Posted: Mon Jun 22, 2009 7:04 am
by jmestep
OK, I will try that. Thanks.