BasketItemDataSource.CreateForProduct Returning Null?

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
sweeperq
Commodore (COMO)
Commodore (COMO)
Posts: 497
Joined: Tue Jan 03, 2006 2:45 pm

BasketItemDataSource.CreateForProduct Returning Null?

Post by sweeperq » Fri Nov 18, 2011 12:31 pm

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?

plugables
Captain (CAPT)
Captain (CAPT)
Posts: 276
Joined: Sat Aug 15, 2009 4:04 am
Contact:

Re: BasketItemDataSource.CreateForProduct Returning Null?

Post by plugables » Tue Nov 22, 2011 6:36 am

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

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: BasketItemDataSource.CreateForProduct Returning Null?

Post by AbleMods » Wed Nov 23, 2011 9:16 am

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 :)
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

sweeperq
Commodore (COMO)
Commodore (COMO)
Posts: 497
Joined: Tue Jan 03, 2006 2:45 pm

Re: BasketItemDataSource.CreateForProduct Returning Null?

Post by sweeperq » Wed Nov 23, 2011 10:00 am

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.

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: BasketItemDataSource.CreateForProduct Returning Null?

Post by AbleMods » Wed Nov 23, 2011 10:10 am

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.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

Re: BasketItemDataSource.CreateForProduct Returning Null?

Post by jmestep » Fri Nov 25, 2011 10:55 am

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
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

sweeperq
Commodore (COMO)
Commodore (COMO)
Posts: 497
Joined: Tue Jan 03, 2006 2:45 pm

Re: BasketItemDataSource.CreateForProduct Returning Null?

Post by sweeperq » Mon Nov 28, 2011 2:41 pm

Joe, exactly!

Thanks for the info Judy.

Post Reply