Making selected variant image show in basket

Store UI, layout, design, look and feel; Discussion on the customer facing pages of your online store. Cascading Style Sheets, Themes, Scriptlets, NVelocity and the components in the ConLib directory.
User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Making selected variant image show in basket

Post by AbleMods » Mon Feb 11, 2008 12:05 am

Introduction
When a product is added to the basket, an image is displayed along with the item details. However, adding a variant option does not display the variants image, it displays the main product image. This can be confusing to customers who ordered a Red shirt and the basket shows them the green shirt. This modification forces the MiniBasket and Basket pages to show the variant image.

This modification assumes AC7 RC2.

Requirements
For these modifications to work, you must always have your swatch variant as the first set of choices. Any number of choice sets are allowed, but the first set must be the swatches. For non-swatch variants or if the first choice set does not contain an image URL, the main product image will always be displayed.
The code uses the "Thumbnail" URL from the variant choices setup page to display the swatch in the minibasket. What this means is you are going to have to use icon-sized swatches. There's no swatch field for icon, so put your icon URL in the thumbnail swatch field. This is because the minibasket doesn't like images that aren't "mini", hence the name.

File Changes

First, make a back up of your ~/ConLib/ folder.

Edit your ~/ConLib/Minibasket.ascx.cs and replace the entire GetIconUrl function with this code:

Code: Select all

    protected string GetIconUrl(Object obj)
    {
        BasketItem bitem = obj as BasketItem;
        if (bitem != null)
        {
            //We've got a valid basket item.  Now let's see if we should show the product image
            //or the variant image.
            if (bitem.ProductVariantId != 0)
            {
                //Variant detected.  Load the first option that has an image URL and return it
                OptionChoice _TVar = OptionChoiceDataSource.Load(bitem.ProductVariant.Option1);
                if (!string.IsNullOrEmpty(_TVar.ThumbnailUrl))
                {
                    return _TVar.ThumbnailUrl;
                }
                else if (!string.IsNullOrEmpty(_TVar.ImageUrl))
                {
                    return _TVar.ImageUrl;
                }
                else
                {
                    return bitem.Product.ImageUrl;
                }


            }
            else
            {
                if (!string.IsNullOrEmpty(bitem.Product.IconUrl))
                {
                    return bitem.Product.IconUrl;
                }
                else if (!string.IsNullOrEmpty(bitem.Product.ThumbnailUrl))
                {
                    return bitem.Product.ThumbnailUrl;
                }
                else
                {
                    return bitem.Product.ImageUrl;
                }
            }
        }
        return "";
    }
Save it.

Now edit your ~/ConLib/Basket.ascx.cs file and replace the entire ShowProductImagePanel function with this code:

Code: Select all

    protected bool ShowProductImagePanel(object dataItem)
    {
        BasketItem item = (BasketItem)dataItem;
        return ((item.OrderItemType == OrderItemType.Product) && (!string.IsNullOrEmpty(item.Product.ThumbnailUrl)));
    }

    protected String ProductImage(object dataItem)
    {
        BasketItem item = (BasketItem)dataItem;
        if (item.ProductVariantId != 0)
        {
            // Basket item has variants.  If the first variant choice has an image URL,
            // use it instead of the regular product image.
            OptionChoice _Tvar = OptionChoiceDataSource.Load(item.ProductVariant.Option1);
            if (_Tvar.ThumbnailUrl != "")
            {
                return _Tvar.ThumbnailUrl;
            }
            else
            {
                return item.Product.ThumbnailUrl;
            }
        
        }
        else
        {
            return item.Product.ThumbnailUrl;
        }
    }
Next, add this line to the class definitions at the top of the minibasket.ascx.cs file - anywhere is fine as long as it's on its own line like the others.

Code: Select all

using CommerceBuilder.Products; 
Save it.

Finally, edit the ~/ConLib/Basket.ascx file and find this line:

Code: Select all

<asp:Image ID="Thumbnail" runat="server" AlternateText='<%# Eval("Product.Name") %>' ImageUrl='<%#Eval("Product.ThumbnailURL")%>' EnableViewState="false" />
and replace it with this line:

Code: Select all

<asp:Image ID="Thumbnail" runat="server" AlternateText='<%# Eval("Product.Name") %>' ImageUrl='<%#MyProductImage(Container.DataItem)%>' EnableViewState="false" />
Save it.

What Just Happened
What you've done is modified the minibasket and basket pages to test to the items in the basket. If the item has a variant, the page will look up the selected variant and retrieve the image specified. If the first set of variant choices does not have an image URL, the product image is used instead.

Conclusion
I am still extensively testing this modification. Please do the same yourself and report any issues here. I will make the necessary changes to the post if any problems arise. Now, if anyone has any hand sanitizer, I'd really like to get this C-Sharp off my hands and get back to my VB programming :wink:
Last edited by AbleMods on Fri May 02, 2008 9:18 pm, edited 3 times in total.
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
Road Rider
Commander (CMDR)
Commander (CMDR)
Posts: 144
Joined: Sat Jan 26, 2008 12:43 pm
Contact:

Post by Road Rider » Wed Feb 13, 2008 7:23 am

Joe:

Hey finally got around to giving this a try this morning. Ah.... no workie. I do not seem to have .aspx.cs or .aspx files, I have .ascx.cs and .ascx (no p only c). Why would that be?
Doug Morrison
Director of Marketing and eCommerce
Bike Authority
http://www.bikeauthority.com

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

Post by AbleMods » Wed Feb 13, 2008 7:36 am

Sorry, typos on my part. I've corrected the post.

User controls always have a .ascx extension. Standard .Net pages have an .aspx extension. When you're staring at this stuff for hours at a time, it kinda blurs after a while.
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
Road Rider
Commander (CMDR)
Commander (CMDR)
Posts: 144
Joined: Sat Jan 26, 2008 12:43 pm
Contact:

Post by Road Rider » Wed Feb 13, 2008 8:57 am

10-4, I know what you mean about staring at it for hours (I didnt used to be cross eyed).

OK then my issue with it not working must have been a miss copy on my end.
Doug Morrison
Director of Marketing and eCommerce
Bike Authority
http://www.bikeauthority.com

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

Post by AbleMods » Wed Feb 13, 2008 9:07 am

Maybe it would have been easier if I posted a .zip file for the changed files.

I've been trying to get away from that though, so people could potentially use the changes in future versions of AC7.
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
Road Rider
Commander (CMDR)
Commander (CMDR)
Posts: 144
Joined: Sat Jan 26, 2008 12:43 pm
Contact:

Post by Road Rider » Wed Feb 13, 2008 9:10 am

OK, I have tried this twice now with the same issue. The following is what displays in the minibasket:

[[ConLib:MiniBasket]] d:\FTP\bikeauth\Htdocs\ablecomm\ConLib\MiniBasket.ascx.cs(139): error CS0246: The type or namespace name 'OptionChoice' could not be found (are you missing a using directive or an assembly reference?)

Hmmm?
Doug Morrison
Director of Marketing and eCommerce
Bike Authority
http://www.bikeauthority.com

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

Post by AbleMods » Wed Feb 13, 2008 9:33 am

Look at the top of the minibasket.ascx.cs file and see if this line is there:

Code: Select all

using CommerceBuilder.Products;
if it's not, add it below this line:

Code: Select all

using CommerceBuilder.Orders;
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
Road Rider
Commander (CMDR)
Commander (CMDR)
Posts: 144
Joined: Sat Jan 26, 2008 12:43 pm
Contact:

Post by Road Rider » Wed Feb 13, 2008 9:46 am

You da man!!! Seems to work perfectly.
Doug Morrison
Director of Marketing and eCommerce
Bike Authority
http://www.bikeauthority.com

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

Post by AbleMods » Wed Feb 13, 2008 9:58 am

I'll edit the post, my bad - thought that class was already included.

Sorry for the confusion.
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
Road Rider
Commander (CMDR)
Commander (CMDR)
Posts: 144
Joined: Sat Jan 26, 2008 12:43 pm
Contact:

Post by Road Rider » Wed Feb 13, 2008 10:15 am

No apologies needed what so ever.
Doug Morrison
Director of Marketing and eCommerce
Bike Authority
http://www.bikeauthority.com

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

Re: Making selected variant image show in basket

Post by AbleMods » Thu May 01, 2008 5:14 pm

Revisions to support RC3

For the minibasket.ascx.cs change, use this code instead:

Code: Select all

    protected string GetIconUrl(Object obj)
    {
        BasketItem bitem = obj as BasketItem;
        if (bitem != null)
        {
            //BEGIN: Solunar Mod
            //We've got a valid basket item.  Now let's see if we should show the product image 
            //or the variant image. 
            
            if (bitem.ProductVariant!= null)
            {
                //Variant detected.  Load the first option that has an image URL and return it 
                OptionChoice _TVar = OptionChoiceDataSource.Load(bitem.ProductVariant.Option1);
                if (!string.IsNullOrEmpty(_TVar.ThumbnailUrl))
                {
                    return _TVar.ThumbnailUrl;
                }
                else if (!string.IsNullOrEmpty(_TVar.ImageUrl))
                {
                    return _TVar.ImageUrl;
                }
                else
                {
                    return bitem.Product.ImageUrl;
                }


            }
            else
            {
                if (!string.IsNullOrEmpty(bitem.Product.IconUrl))
                {
                    return bitem.Product.IconUrl;
                }
                else if (!string.IsNullOrEmpty(bitem.Product.ThumbnailUrl))
                {
                    return bitem.Product.ThumbnailUrl;
                }
                else
                {
                    return bitem.Product.ImageUrl;
                }
            }
            //END: Solunar Mod
        }
        return "";
    }
And use this for the basket.ascx.cs changes instead:

Code: Select all

    protected bool ShowProductImagePanel(object dataItem)
    {
        BasketItem item = (BasketItem)dataItem;
        return ((item.OrderItemType == OrderItemType.Product) && (!string.IsNullOrEmpty(item.Product.ThumbnailUrl)));
    }

    //BEGIN: Solunar Mod
    protected String MyProductImage(object dataItem)
    {
        BasketItem item = (BasketItem)dataItem;
        
        if (item.ProductVariant != null)
        {
            // Basket item has variants.  If the first variant choice has an image URL, 
            // use it instead of the regular product image. 
            OptionChoice _Tvar = OptionChoiceDataSource.Load(item.ProductVariant.Option1);
            if (_Tvar.ThumbnailUrl != "")
            {
                return _Tvar.ThumbnailUrl;
            }
            else
            {
                return item.Product.ThumbnailUrl;
            }

        }
        else
        {
            return item.Product.ThumbnailUrl;
        }
    }
    //END: Solunar Mod
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

Mike31523
Ensign (ENS)
Ensign (ENS)
Posts: 3
Joined: Tue Apr 08, 2008 7:33 am

Re: Making selected variant image show in basket

Post by Mike31523 » Fri May 02, 2008 11:33 am

Beautiful! Works perfect! Thanks for your work on this!

One thing... When you edit ConLib/Basket.ascx make sure you change the ImageURL value to ImageUrl='<%#MyProductImage(Container.DataItem)%>' instead of ImageUrl='<%#ProductImage(Container.DataItem)%>' or you can just change it in basket.asx.cs.

Thanks again!

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

Re: Making selected variant image show in basket

Post by AbleMods » Fri May 02, 2008 9:19 pm

Ohhh, good catch!

OP edited :)
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
cerami2
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 103
Joined: Thu Nov 08, 2007 5:29 am
Location: Plymouth MN
Contact:

Re: Making selected variant image show in basket help

Post by cerami2 » Mon May 05, 2008 5:56 pm

hi joe this is a great add on
thanks Joe Cerami
Last edited by cerami2 on Tue May 06, 2008 4:07 pm, edited 1 time in total.

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

Re: Making selected variant image show in basket

Post by AbleMods » Tue May 06, 2008 5:54 am

What's the error you are receiving?
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
cerami2
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 103
Joined: Thu Nov 08, 2007 5:29 am
Location: Plymouth MN
Contact:

Re: Making selected variant image show in basket

Post by cerami2 » Tue May 06, 2008 6:03 am

12345678
Last edited by cerami2 on Tue May 06, 2008 4:09 pm, edited 1 time in total.

User avatar
cerami2
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 103
Joined: Thu Nov 08, 2007 5:29 am
Location: Plymouth MN
Contact:

Re: Making selected variant image show in basket

Post by cerami2 » Tue May 06, 2008 6:42 am

I put back the original files and i still get the error I am getting an error that is not from the change of code .Any Ideas
Thanks

when i click on an item that has no variations I get the error but when i goto the home page were the mini basket It added it to the cart.

this is happening on http://www.insulincase.com
Last edited by cerami2 on Tue May 06, 2008 7:03 am, edited 1 time in total.

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

Re: Making selected variant image show in basket

Post by AbleMods » Tue May 06, 2008 6:55 am

No idea at all. Somewhere is another change that isn't correct and causing the problem. No way to tell. Make sure when you "put it back" you restore both the .ascx and the .ascx.cs files.
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
cerami2
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 103
Joined: Thu Nov 08, 2007 5:29 am
Location: Plymouth MN
Contact:

Re: Making selected variant image show in basket

Post by cerami2 » Tue May 06, 2008 7:23 am

well I will reinstall the website fresh and see if it works then I will reinstall all my custom pages one at a time and see if the creat the error or could it be a database problem?
:?

Thanks Joe for your input

Joe

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

Re: Making selected variant image show in basket

Post by AbleMods » Tue May 06, 2008 7:39 am

You don't have a recent backup that includes your customizations?
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
batmike
Commander (CMDR)
Commander (CMDR)
Posts: 123
Joined: Tue Sep 04, 2007 10:46 am
Location: Minneapolis, MN
Contact:

Re: Making selected variant image show in basket

Post by batmike » Tue May 06, 2008 8:08 am

Thanks for the customization! Works great for me. Definitely an important fix for our products.

Mike

User avatar
cerami2
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 103
Joined: Thu Nov 08, 2007 5:29 am
Location: Plymouth MN
Contact:

Re: Making selected variant image show in basket

Post by cerami2 » Tue May 06, 2008 4:01 pm

got it all workin now... happy camper
Joe :D

User avatar
cerami2
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 103
Joined: Thu Nov 08, 2007 5:29 am
Location: Plymouth MN
Contact:

Re: Making selected variant image show in basket

Post by cerami2 » Fri Jul 04, 2008 8:31 pm

guess what i broke the basket.aspx.cs :x :(

i followed the directions but i keep getting this error on the basket.aspx with the browser

[ConLib:Basket]] d:\Domains\insulincase.com\wwwroot\ConLib\Basket.ascx.cs(271): error CS1513: } expected

I am using the final


Thanks Joe

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

Re: Making selected variant image show in basket

Post by AbleMods » Fri Jul 04, 2008 11:26 pm

Welcome to the (one of the many) joy of C-Sharp.

You missed a "{" or a "}" somewhere and it's throwing the compiler off.

Be aware though, those mods weren't designed for final AC7 - I wrote those for RC2 and there may have been (incompatible) changes to the basket page since then in the final release.
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

musthavebag
Ensign (ENS)
Ensign (ENS)
Posts: 9
Joined: Fri Sep 12, 2008 8:41 am

Re: Making selected variant image show in basket

Post by musthavebag » Tue Apr 14, 2009 10:03 am

Hi Joe,
We have the same need - a user can't pick a yellow purse and then see the default orange image for that item.
I tried to implement your solution above but we are running 7.0.2 build 11659. We are not using the mini basket so I suppose I do not need to modify those files. But when we go to the shopping cart after implementing your solution, we get the following error:

[ConLib:Basket]] d:\Inetpub\test\ConLib\Basket.ascx.cs(250): error CS0117: 'CommerceBuilder.Orders.BasketItem' does not contain a definition for 'ProductVariantId

Any ideas? Thanks!

Christina Brandstrom

Post Reply