Showing price range on variant products

This forum is where we'll mirror posts that are of value to the community so they may be more easily found.
Post Reply
User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Showing price range on variant products

Post by AbleMods » Mon Nov 29, 2010 9:45 pm

Introduction

This modification will change how your product prices are displayed for variant products. As of AC 7.0.5, variant products will show a zero amount for the price until a variant selection has been made. This is only mildly confusing if the shopper is on the product page, but horrible if they're browsing your categories. Nobody (who wants to stay in business) likes to show a zero price. Ever.

So I came up with a way to make the product price control show the range of prices possible for a given variant product. For example, your variant product has 5 choices priced from $9.95 all the way to $ 124.95. Out of the box, AC7 will always show this product with a zero price until a variant selection is made. This happens both in the category and product page displays. With my modification, prices will now display as a range i.e. "$ 9.95 - $ 124.95". That looks much more elegant than "$ 0.00".

Files to Modify
As always, time for the disclaimer. Back up any files you modify before you make changes to them. That way any damage done can be easily reverted to a working copy. With that said, let's make the change....

Edit the /ConLib/Utility/ProductPrice.ascx.cs user control file. Find this code:

Code: Select all

                    //SHOW THE PRICE
                    if (pcalc.AppliedSpecial != null && pcalc.AppliedSpecial.EndDate != DateTime.MinValue)
                    {
                        // SHOW THE BASE PRICE AND SPECIAL PRICE
                        RetailPrice1.Text = string.Format(_BasePriceFormat, basePriceWithVAT);
                        Price.Text = string.Format(_SpecialPriceFormat, priceWithVAT, pcalc.AppliedSpecial.EndDate);
                    }
                    else
                    {
                        Price.Text = string.Format(_PriceFormat, priceWithVAT);
                    }
and replace it with this code:

Code: Select all

                    //SHOW THE PRICE
                    if (pcalc.AppliedSpecial != null && pcalc.AppliedSpecial.EndDate != DateTime.MinValue)
                    {
                        // SHOW THE BASE PRICE AND SPECIAL PRICE
                        RetailPrice1.Text = string.Format(_BasePriceFormat, basePriceWithVAT);
                        Price.Text = string.Format(_SpecialPriceFormat, priceWithVAT, pcalc.AppliedSpecial.EndDate);
                    }
                    else
                    {
                        Price.Text = string.Format(_PriceFormat, priceWithVAT);
                    }

                    // BEGIN MOD:  AbleMods.com
                    // 8/10/2010
                    // Show range of prices for the price if variants are involved
                    if (_Product.Variants.Count > 0 && _OptionList == "")
                    {
                        // find only the available variants for this product
                        ProductVariantCollection _Variants = new ProductVariantCollection();
                        foreach (ProductVariant _Variant in _Product.Variants)
                            if (_Variant.Available)
                                _Variants.Add(_Variant);

                        // grab the lowest and highest prices in the collection
                        LSDecimal _LowestPrice = 0;
                        LSDecimal _HighestPrice = 0;

                        foreach (ProductVariant _Variant in _Variants)
                        {
                            if (_Variant.Price > _HighestPrice)
                                _HighestPrice = _Variant.Price;

                            if (_Variant.Price < _LowestPrice | _LowestPrice == 0)
                                _LowestPrice = _Variant.Price;
                        }

                        // error check - we should have at least 1 variant.
                        // if we don't, log it because we've got a product with NO available variants
                        // makes it kind of hard to buy it :)
                        if (_Variants.Count < 1)
                        {
                            ErrorMessageDataSource.Insert(new ErrorMessage(MessageSeverity.Warn, "AbleMods", "Product <a href='/admin/products/editproduct.aspx?ProductId=" + _Product.ProductId + "'>" + _Product.Name + "</a> has no available variants for sale"));
                            Price.Text = "unavailable";
                        }
                        else
                        {

                            // Show the lowest price first
                            Price.Text = String.Format(_PriceFormat, _LowestPrice);

                            // if there is a range of prices available, show the high end on the price as well
                            if (_LowestPrice != _HighestPrice)
                                Price.Text += String.Format(" - " + _PriceFormat, _HighestPrice);

                        }
                    }
                    // don't show anything if it's a zero-price
                    if (Price.Text == "$0.00")
                        Price.Text = "";
                    // END MOD:  AbleMods.com
Now save it and upload to the /ConLib/Utility/ folder. Check out your site - you should see your variant products show a far more meaningful price display than it used to show.

You can see a working example of this modification on my site here: http://www.solunar.com/Rifles-C1125.asp ... %20ASC&p=3

The nice thing about this modification is the ProductPrice user control is referenced in many places. So all of them should be updated with this one modification.

Enjoy ! :)
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

JessHessing1989
Ensign (ENS)
Ensign (ENS)
Posts: 1
Joined: Wed Dec 01, 2010 9:59 pm

Re: Showing price range on variant products

Post by JessHessing1989 » Wed Dec 01, 2010 10:42 pm

thanks for the info

Abinator
Ensign (ENS)
Ensign (ENS)
Posts: 14
Joined: Thu Feb 19, 2009 2:12 pm

Re: Showing price range on variant products

Post by Abinator » Wed Apr 27, 2011 9:03 pm

First let me say thanks for posting this mod. I implemented the change as you have listed, but it isn't consistently working. The strange thing is that it seems to work on some products, but not all. I have opened up two products (one working and the other not) and compared every property on the two products and they appear to be identical. What could I be missing?

Here is a link to my site that shows what I am talking about: http://www.bluegrasscandlesupply.com/Al ... st-C8.aspx

I understand if you don't have time to support this free mod, but I would appreciate the help if you could.

Thanks,
Jim

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

Re: Showing price range on variant products

Post by AbleMods » Thu Apr 28, 2011 7:06 am

Jim,

Thanks for the feedback. You are correct, the revision doesn't *always* show the range as it should.

I couldn't quite figure out why, aside from some sort of caching that AbleCommerce may be doing in the background. Nonetheless, I have done some testing and found a different way to accomplish the same goal. I tested the daylights out of this and it seems to work more consistently. Here's the new code:

Code: Select all

                    //SHOW THE PRICE
                    if (pcalc.AppliedSpecial != null && pcalc.AppliedSpecial.EndDate != DateTime.MinValue)
                    {
                        // SHOW THE BASE PRICE AND SPECIAL PRICE
                        RetailPrice1.Text = string.Format(_BasePriceFormat, basePriceWithVAT);
                        Price.Text = string.Format(_SpecialPriceFormat, priceWithVAT, pcalc.AppliedSpecial.EndDate);
                    }
                    else
                    {
                        Price.Text = string.Format(_PriceFormat, priceWithVAT);
                    }

                    // BEGIN MOD:  AbleMods.com
                    // 4/28/2011
                    // Show range of prices for the price if variants are involved
                    if (_Product.Variants.Count > 0 && _OptionList == "")
                    {
                        // find only the available variants for this product
                        // grab the lowest and highest prices in the collection
                        LSDecimal _LowestPrice = 0;
                        LSDecimal _HighestPrice = 0;
                        
                        ProductVariantManager _Mgr = new ProductVariantManager(_Product.ProductId);
                        for (int i = 0; i < _Mgr.Count; i++)
                        {
                            ProductVariant _Variant = _Mgr.GetVariantByIndex(i);
                            if (_Variant.Price > _HighestPrice)
                                _HighestPrice = _Variant.Price;

                            if (_Variant.Price < _LowestPrice | _LowestPrice <= 0)
                                _LowestPrice = _Variant.Price;
                        }

                        // error check - we should have at least 1 variant.
                        // if we don't, log it because we've got a product with NO available variants
                        // makes it kind of hard to buy it :)
                        if (_Mgr.Count < 1)
                        {
                            ErrorMessageDataSource.Insert(new ErrorMessage(MessageSeverity.Warn, "AbleMods", "Product <a href='/admin/products/editproduct.aspx?ProductId=" + _Product.ProductId + "'>" + _Product.Name + "</a> has no available variants for sale"));
                            Price.Text = "unavailable";
                        }
                        else
                        {

                            // Show the lowest price first
                            Price.Text = String.Format(_PriceFormat, _LowestPrice);

                            // if there is a range of prices available, show the high end on the price as well
                            if (_LowestPrice != _HighestPrice)
                                Price.Text += String.Format(" - " + _PriceFormat, _HighestPrice);

                        }
                    }
                    // don't show anything if it's a zero-price
                    if (Price.Text == "$0.00")
                        Price.Text = "";
                    // END MOD:  AbleMods.com
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

Abinator
Ensign (ENS)
Ensign (ENS)
Posts: 14
Joined: Thu Feb 19, 2009 2:12 pm

Re: Showing price range on variant products

Post by Abinator » Thu Apr 28, 2011 11:55 am

Joe,

You Rock! That works much better... in fact, it helped me see a few products that didn't have the Variant Price Override set right. Not only will this Mod help sell more, but it has probably saved me money by pointing out my mistakes too! Thanks again.

Jim

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

Re: Showing price range on variant products

Post by AbleMods » Thu Apr 28, 2011 12:47 pm

No problem, glad to help.
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: Showing price range on variant products

Post by sweeperq » Wed Aug 17, 2011 9:54 am

Tried the code and it wouldn't work for me on 7.0.7. For some reason, when I ran through debug mode, _Product.Variants is null (even though I have product options and option choices with price and sku modifiers). Ended up doing this:

Code: Select all

					if (_Product != null)
					{
						LSDecimal _LowestPrice = priceWithVAT;
						LSDecimal _HighestPrice = priceWithVAT;

						foreach (ProductOption opti in _Product.ProductOptions)
						{
							foreach (OptionChoice choi in opti.Option.Choices)
							{
								if (_LowestPrice > priceWithVAT + choi.PriceModifier)
									_LowestPrice = priceWithVAT + choi.PriceModifier;
								if (_HighestPrice < priceWithVAT + choi.PriceModifier)
									_HighestPrice = priceWithVAT + choi.PriceModifier;
							}
						}
						if (_LowestPrice != _HighestPrice)
						{
							Price.Text = string.Format(_PriceFormat, _LowestPrice) + " - " + string.Format(_PriceFormat, _HighestPrice);
						}
					}

Post Reply