Product Attributes DropdownList Issues

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
14 years ago
I am not sure if this is the correct forum, if not my apologies. I have created a dropdownlist for a magazine subscription issues, multivalues, I had to set the price in product variant(SKU) to 0.00 (zero) for the correct pricing to populate in the cart. The problem is that 0.00(USD) shows on category Page (product price) and the product details page. The price does not change when the dropdownlist selection changes and it does not use the default value of the preselected item.

I did add code to make the dropdownlist.autopostback true, I think I also need to create an slectedIndex change event handler for the dropdownlist. As I am new to using this product, I am not totally familar with the technology, not sure if when the category page is generated is AJAX involved and if so, will I need to add a trigger?

Any help would be appreciated..

Regards, Keith
14 years ago
An update for those who need the productVariantAttribute(s) to manage the price label, the price does show correctly in the Cart, however those of you who want the price to change on the Product Listing and Details Page when a dropdown list item is selected, try the following..


In productsAttribute.ascx.cs, Procedure CreateAttributesControls(), switch (attribute.AttributeControlType),case AttributeControlTypeEnum.DropdownList:
Makes sure you have not added or set ddlAttributes.autopostBack = true, REMOVE it  or set it to FALSE

After the line divAttribute.Controls.Add(ddlAttributes);,
ADD ddlAttributes.Attributes.Add("onChange", "onSubscriptionChange(this);");


Add Javascript function and var declaration, I added a js file in the scripts folder with function and var declaration referenced it in the /MasterPages/Root.Master<head><script language="javascript" src="../scripts/Common.js" type="text/javascript"></script></head>, but you can do whatever.

var LabelPriceValueClientID = 'ctl00_ctl00_cph1_cph1_ctl00_ctrlProductVariantsInGrid_rptVariants_ctl00_ctrlProductPrice_lblPriceValue';

function onSubscriptionChange(ctrl) {
    var obj = document.getElementById(ctrl.id.toString());
    var x = obj.selectedIndex;
    var text = obj.options[x].text;
    var start = text.indexOf("$");
    var end = text.indexOf("]");
    var amount = text.substring(start, end);
    var span = document.getElementById(LabelPriceValueClientID);
    start = span.innerHTML.indexOf("(");
    end = span.innerHTML.indexOf(")");
    var currencyType = span.innerHTML.substring(start, end + 1);
    span.innerHTML = amount + " " + currencyType;
}

AND you will need to
create a function in /modules/ProductPrice.ascx.cs, ProductBox1.ascx, productBox.ascx

that does the initial population of the lblPriceValue, you need to create a function similar to

private decimal GetPriceFromProductAttributeDropDownListIfExits() //ProductPrice.ascx.cs
private decimal GetPriceFromProductAttributeDropDownListIfExits(int ProductVariantID) //ProductBox1 and 2 .ascx.cs

        {
            decimal initialDropDownListPrice = (decimal)0.00;
            ProductVariant productVariant = ProductManager.GetProductVariantByID(this.ProductVariantID);
            if (productVariant != null)
            {
                ProductVariantAttributeCollection productVariantAttributes = productVariant.ProductVariantAttributes;
                if (productVariantAttributes.Count > 0)
                {
                    foreach (ProductVariantAttribute attribute in productVariantAttributes)
                    {
                        switch (attribute.AttributeControlType)
                        {
                            case AttributeControlTypeEnum.DropdownList:
                                {
                                    ProductVariantAttributeValueCollection pvaValues = attribute.ProductVariantAttributeValues;
                                    foreach (ProductVariantAttributeValue pvaValue in pvaValues)
                                    {
                                        //string pvaValueName = pvaValue.Name;
                                        if (pvaValue.IsPreSelected)
                                        {
                                            decimal priceAdjustmentBase = TaxManager.GetPrice(productVariant, pvaValue.PriceAdjustment);
                                            decimal priceAdjustment = CurrencyManager.ConvertCurrency(priceAdjustmentBase, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                            if (priceAdjustmentBase > decimal.Zero)
                                            {
                                                initialDropDownListPrice = priceAdjustment;
                                                //pvaValueName += string.Format(" [+{0}]", PriceHelper.FormatPrice(priceAdjustment, false, false));
                                                break;
                                            }
                                        }
                                    }
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }

            return initialDropDownListPrice;

        }

This will be called from BindData()

                        if (finalPriceWithoutDiscountBase != oldPriceBase && oldPriceBase != decimal.Zero)
                        {
                            lblOldPrice.Text = PriceHelper.FormatPrice(oldPrice);
                            lblPrice.Text = PriceHelper.FormatPrice(finalPriceWithoutDiscount);
                        }
                        else
                        {
                            lblOldPrice.Visible = false;
                            if (finalPriceWithoutDiscountBase == 0)
                            {
                                NEW NEW finalPriceWithoutDiscount = GetPriceFromProductAttributeDropDownListIfExits(productVariant.ProductVariantID);//ProductBox1 and 2 ascx.cs
GetPriceFromProductAttributeDropDownListIfExits();// ProductPrice.ascx.cs
                            }

                            lblPrice.Text = PriceHelper.FormatPrice(finalPriceWithoutDiscount);
                            //lblOldPrice.Visible = false;
                            //lblPrice.Text = PriceHelper.FormatPrice(finalPriceWithoutDiscount);
                        }

Make sure you have added using NopSolutions.NopCommerce.BusinessLogic.Products.Attributes;
To ProductPrice.ascx.cs, ProductBox1 and 2 ascx.cs
14 years ago
Keith, thanks!
14 years ago
My pleasure..........Hope it helps others..
12 years ago
Hi, Thanks for the post. I need the same functionality in nopcommerce v 2.2.

Please help me with your inputs.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.