Page 1 of 1

BasketItemDataSource.CreateForProduct Returning Null?

Posted: Fri Nov 18, 2011 12:31 pm
by sweeperq
We have a gift card on one of our websites. It is set up as follows:
  • Visibility = Hidden (so it doesn't show up in search or in site categories)
  • Max Quantity = 1 (only 1 per order)
  • Product Template Includes 1 Customer TextBox Field for inputing gift message
We have a link to it on the shopping cart once an item is placed in the cart. If the user goes back to the product page and tries to add another, instead of updating the existing card or simply showing a message saying only 1 is allowed, a Null Reference is thrown.

I tracked down the error and it is occurring in the GetBasketItem() function of the BuyProductDialog.ascx.cs file. It occurs because BasketItemDataSource.CreateForProduct returns null for the product when set up as mentioned above. Unfortunately, the next line of code is basketItem.BasketId = basket.BasketId (which throws the null reference error because basketItem is null so it has no BasketId property).

Any ideas on how best to handle this situation?

Re: BasketItemDataSource.CreateForProduct Returning Null?

Posted: Tue Nov 22, 2011 6:36 am
by plugables
Place the subsequent code inside a null check

Code: Select all


if(basketItem != null)
{
   //the normal code
}
else
{
  // handle the special case here. Set a message to be shown to the customer.
}

Re: BasketItemDataSource.CreateForProduct Returning Null?

Posted: Wed Nov 23, 2011 9:16 am
by AbleMods
sweeperq wrote:Any ideas on how best to handle this situation?
I guess the bigger question is: Do you want the gift card visible prior to checkout or just when shopper reaches the checkout page?

I've made similar customizations and have found it's best to add stuff like that either during initial load of checkout or immediately following checkout in the void CheckedOut() event. Keeps Able from messing with the basket when I'm trying to mess with the basket :)

Re: BasketItemDataSource.CreateForProduct Returning Null?

Posted: Wed Nov 23, 2011 10:00 am
by sweeperq
What I ended up doing was a null check right after CreateForProduct. If the BasketItem it returns null, I find the existing item in the basket and remove it, then rerun the CreateForProduct. Since they can only have one of the cards in the basket, it is essentially "overwriting" the previous card. Seems to be working like a charm.

Re: BasketItemDataSource.CreateForProduct Returning Null?

Posted: Wed Nov 23, 2011 10:10 am
by AbleMods
Awesome, thanks for posting your solution.

So CreateForProduct() was seeing the product only permitted 1 in the basket and returned null trying to create another basket item of the same product?

Seems like that check should be on the .Add() method for the basket object and not CreateForProduct()....interesting.

Re: BasketItemDataSource.CreateForProduct Returning Null?

Posted: Fri Nov 25, 2011 10:55 am
by jmestep
There is a bug with the max quantity code - I ran across it in a 7.0.3 site and Able patched it for SR1 of 7.0.7. I think without the patch you have to set both a min and max quantity, but I had already implemented a null check in the code I was doing.
http://help.ablecommerce.com/upgrades/a ... ease_1.htm

Re: BasketItemDataSource.CreateForProduct Returning Null?

Posted: Mon Nov 28, 2011 2:41 pm
by sweeperq
Joe, exactly!

Thanks for the info Judy.