示例#1
0
        public void CreateAttributeControls()
        {
            this.phAttributes.Controls.Clear();

            var checkoutAttributes = GetCheckoutAttributes();

            if (checkoutAttributes.Count > 0)
            {
                this.Visible = true;
                foreach (var attribute in checkoutAttributes)
                {
                    var divAttribute   = new Panel();
                    var attributeTitle = new Label();
                    if (attribute.IsRequired)
                    {
                        attributeTitle.Text = "<span>*</span> ";
                    }

                    //text prompt / title
                    string textPrompt = string.Empty;
                    if (!string.IsNullOrEmpty(attribute.LocalizedTextPrompt))
                    {
                        textPrompt = attribute.LocalizedTextPrompt;
                    }
                    else
                    {
                        textPrompt = attribute.LocalizedName;
                    }

                    attributeTitle.Text += Server.HtmlEncode(textPrompt);
                    attributeTitle.Style.Add("font-weight", "bold");

                    bool addBreak = true;
                    switch (attribute.AttributeControlType)
                    {
                    case AttributeControlTypeEnum.TextBox:
                    {
                        addBreak = false;
                    }
                    break;

                    default:
                        break;
                    }
                    if (addBreak)
                    {
                        attributeTitle.Text += "<br />";
                    }
                    else
                    {
                        attributeTitle.Text += "&nbsp;&nbsp;&nbsp;";
                    }
                    divAttribute.Controls.Add(attributeTitle);

                    string controlId = attribute.CheckoutAttributeId.ToString();
                    switch (attribute.AttributeControlType)
                    {
                    case AttributeControlTypeEnum.DropdownList:
                    {
                        //add control items
                        var ddlAttributes = new DropDownList();
                        ddlAttributes.ID = controlId;
                        if (!attribute.IsRequired)
                        {
                            ddlAttributes.Items.Add(new ListItem("---", "0"));
                        }
                        var caValues = attribute.CheckoutAttributeValues;

                        bool preSelectedSet = false;
                        foreach (var caValue in caValues)
                        {
                            string caValueName = caValue.LocalizedName;
                            if (!this.HidePrices)
                            {
                                decimal priceAdjustmentBase = TaxManager.GetCheckoutAttributePrice(caValue);
                                decimal priceAdjustment     = CurrencyManager.ConvertCurrency(priceAdjustmentBase, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                if (priceAdjustmentBase > decimal.Zero)
                                {
                                    caValueName += string.Format(" [+{0}]", PriceHelper.FormatPrice(priceAdjustment));
                                }
                            }
                            var caValueItem = new ListItem(caValueName, caValue.CheckoutAttributeValueId.ToString());
                            if (!preSelectedSet && caValue.IsPreSelected)
                            {
                                caValueItem.Selected = caValue.IsPreSelected;
                                preSelectedSet       = true;
                            }
                            ddlAttributes.Items.Add(caValueItem);
                        }

                        //set already selected attributes
                        if (NopContext.Current.User != null)
                        {
                            string selectedCheckoutAttributes = NopContext.Current.User.CheckoutAttributes;
                            if (!String.IsNullOrEmpty(selectedCheckoutAttributes))
                            {
                                //clear default selection
                                foreach (ListItem item in ddlAttributes.Items)
                                {
                                    item.Selected = false;
                                }
                                //select new values
                                var selectedCaValues = CheckoutAttributeHelper.ParseCheckoutAttributeValues(selectedCheckoutAttributes);
                                foreach (var caValue in selectedCaValues)
                                {
                                    foreach (ListItem item in ddlAttributes.Items)
                                    {
                                        if (caValue.CheckoutAttributeValueId == Convert.ToInt32(item.Value))
                                        {
                                            item.Selected = true;
                                        }
                                    }
                                }
                            }
                        }
                        divAttribute.Controls.Add(ddlAttributes);
                    }
                    break;

                    case AttributeControlTypeEnum.RadioList:
                    {
                        var rblAttributes = new RadioButtonList();
                        rblAttributes.ID = controlId;
                        var caValues = attribute.CheckoutAttributeValues;

                        bool preSelectedSet = false;
                        foreach (var caValue in caValues)
                        {
                            string caValueName = caValue.LocalizedName;
                            if (!this.HidePrices)
                            {
                                decimal priceAdjustmentBase = TaxManager.GetCheckoutAttributePrice(caValue);
                                decimal priceAdjustment     = CurrencyManager.ConvertCurrency(priceAdjustmentBase, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                if (priceAdjustmentBase > decimal.Zero)
                                {
                                    caValueName += string.Format(" [+{0}]", PriceHelper.FormatPrice(priceAdjustment));
                                }
                            }
                            var caValueItem = new ListItem(Server.HtmlEncode(caValueName), caValue.CheckoutAttributeValueId.ToString());
                            if (!preSelectedSet && caValue.IsPreSelected)
                            {
                                caValueItem.Selected = caValue.IsPreSelected;
                                preSelectedSet       = true;
                            }
                            rblAttributes.Items.Add(caValueItem);
                        }

                        //set already selected attributes
                        if (NopContext.Current.User != null)
                        {
                            string selectedCheckoutAttributes = NopContext.Current.User.CheckoutAttributes;
                            if (!String.IsNullOrEmpty(selectedCheckoutAttributes))
                            {
                                //clear default selection
                                foreach (ListItem item in rblAttributes.Items)
                                {
                                    item.Selected = false;
                                }
                                //select new values
                                var selectedCaValues = CheckoutAttributeHelper.ParseCheckoutAttributeValues(selectedCheckoutAttributes);
                                foreach (var caValue in selectedCaValues)
                                {
                                    foreach (ListItem item in rblAttributes.Items)
                                    {
                                        if (caValue.CheckoutAttributeValueId == Convert.ToInt32(item.Value))
                                        {
                                            item.Selected = true;
                                        }
                                    }
                                }
                            }
                        }
                        divAttribute.Controls.Add(rblAttributes);
                    }
                    break;

                    case AttributeControlTypeEnum.Checkboxes:
                    {
                        var cblAttributes = new CheckBoxList();
                        cblAttributes.ID = controlId;
                        var caValues = attribute.CheckoutAttributeValues;
                        foreach (var caValue in caValues)
                        {
                            string caValueName = caValue.LocalizedName;
                            if (!this.HidePrices)
                            {
                                decimal priceAdjustmentBase = TaxManager.GetCheckoutAttributePrice(caValue);
                                decimal priceAdjustment     = CurrencyManager.ConvertCurrency(priceAdjustmentBase, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                if (priceAdjustmentBase > decimal.Zero)
                                {
                                    caValueName += string.Format(" [+{0}]", PriceHelper.FormatPrice(priceAdjustment));
                                }
                            }
                            var caValueItem = new ListItem(Server.HtmlEncode(caValueName), caValue.CheckoutAttributeValueId.ToString());
                            caValueItem.Selected = caValue.IsPreSelected;
                            cblAttributes.Items.Add(caValueItem);
                        }

                        //set already selected attributes
                        if (NopContext.Current.User != null)
                        {
                            string selectedCheckoutAttributes = NopContext.Current.User.CheckoutAttributes;
                            if (!String.IsNullOrEmpty(selectedCheckoutAttributes))
                            {
                                //clear default selection
                                foreach (ListItem item in cblAttributes.Items)
                                {
                                    item.Selected = false;
                                }
                                //select new values
                                var selectedCaValues = CheckoutAttributeHelper.ParseCheckoutAttributeValues(selectedCheckoutAttributes);
                                foreach (var caValue in selectedCaValues)
                                {
                                    foreach (ListItem item in cblAttributes.Items)
                                    {
                                        if (caValue.CheckoutAttributeValueId == Convert.ToInt32(item.Value))
                                        {
                                            item.Selected = true;
                                        }
                                    }
                                }
                            }
                        }
                        divAttribute.Controls.Add(cblAttributes);
                    }
                    break;

                    case AttributeControlTypeEnum.TextBox:
                    {
                        var txtAttribute = new TextBox();
                        txtAttribute.Width = SettingManager.GetSettingValueInteger("CheckoutAttribute.Textbox.Width", 300);
                        txtAttribute.ID    = controlId;

                        //set already selected attributes
                        if (NopContext.Current.User != null)
                        {
                            string selectedCheckoutAttributes = NopContext.Current.User.CheckoutAttributes;
                            if (!String.IsNullOrEmpty(selectedCheckoutAttributes))
                            {
                                //clear default selection
                                txtAttribute.Text = string.Empty;

                                //select new values
                                var enteredText = CheckoutAttributeHelper.ParseValues(selectedCheckoutAttributes, attribute.CheckoutAttributeId);
                                if (enteredText.Count > 0)
                                {
                                    txtAttribute.Text = enteredText[0];
                                }
                            }
                        }
                        divAttribute.Controls.Add(txtAttribute);
                    }
                    break;

                    case AttributeControlTypeEnum.MultilineTextbox:
                    {
                        var txtAttribute = new TextBox();
                        txtAttribute.ID       = controlId;
                        txtAttribute.TextMode = TextBoxMode.MultiLine;
                        txtAttribute.Width    = SettingManager.GetSettingValueInteger("CheckoutAttribute.MultiTextbox.Width", 300);
                        txtAttribute.Height   = SettingManager.GetSettingValueInteger("CheckoutAttribute.MultiTextbox.Height", 150);

                        //set already selected attributes
                        if (NopContext.Current.User != null)
                        {
                            string selectedCheckoutAttributes = NopContext.Current.User.CheckoutAttributes;
                            if (!String.IsNullOrEmpty(selectedCheckoutAttributes))
                            {
                                //clear default selection
                                txtAttribute.Text = string.Empty;

                                //select new values
                                var enteredText = CheckoutAttributeHelper.ParseValues(selectedCheckoutAttributes, attribute.CheckoutAttributeId);
                                if (enteredText.Count > 0)
                                {
                                    txtAttribute.Text = enteredText[0];
                                }
                            }
                        }
                        divAttribute.Controls.Add(txtAttribute);
                    }
                    break;

                    case AttributeControlTypeEnum.Datepicker:
                    {
                        var datePicker = new NopDatePicker();
                        //changes these properties in order to change year range
                        datePicker.FirstYear = DateTime.Now.Year;
                        datePicker.LastYear  = DateTime.Now.Year + 1;
                        datePicker.ID        = controlId;
                        divAttribute.Controls.Add(datePicker);
                    }
                    break;

                    default:
                        break;
                    }
                    phAttributes.Controls.Add(divAttribute);
                }
            }
            else
            {
                this.Visible = false;
            }
        }
示例#2
0
        public void CreateAttributeControls()
        {
            var productVariant = ProductManager.GetProductVariantById(this.ProductVariantId);

            if (productVariant != null)
            {
                this.phAttributes.Controls.Clear();
                var productVariantAttributes = productVariant.ProductVariantAttributes;
                if (productVariantAttributes.Count > 0)
                {
                    StringBuilder adjustmentTableScripts = new StringBuilder();
                    StringBuilder attributeScripts       = new StringBuilder();

                    this.Visible = true;
                    foreach (var attribute in productVariantAttributes)
                    {
                        var divAttribute   = new Panel();
                        var attributeTitle = new Label();
                        if (attribute.IsRequired)
                        {
                            attributeTitle.Text = "<span>*</span> ";
                        }

                        //text prompt / title
                        string textPrompt = string.Empty;
                        if (!string.IsNullOrEmpty(attribute.TextPrompt))
                        {
                            textPrompt = attribute.TextPrompt;
                        }
                        else
                        {
                            textPrompt = attribute.ProductAttribute.LocalizedName;
                        }

                        attributeTitle.Text += Server.HtmlEncode(textPrompt);
                        attributeTitle.Style.Add("font-weight", "bold");

                        //description
                        if (!string.IsNullOrEmpty(attribute.ProductAttribute.LocalizedDescription))
                        {
                            attributeTitle.Text += string.Format("<br /><span>{0}</span>", Server.HtmlEncode(attribute.ProductAttribute.LocalizedDescription));
                        }

                        bool addBreak = true;
                        switch (attribute.AttributeControlType)
                        {
                        case AttributeControlTypeEnum.TextBox:
                        {
                            addBreak = false;
                        }
                        break;

                        default:
                            break;
                        }
                        if (addBreak)
                        {
                            attributeTitle.Text += "<br />";
                        }
                        else
                        {
                            attributeTitle.Text += "&nbsp;&nbsp;&nbsp;";
                        }
                        divAttribute.Controls.Add(attributeTitle);
                        phAttributes.Controls.Add(divAttribute);

                        string controlId = string.Format("{0}_{1}", attribute.ProductAttribute.ProductAttributeId, attribute.ProductVariantAttributeId);
                        switch (attribute.AttributeControlType)
                        {
                        case AttributeControlTypeEnum.DropdownList:
                        {
                            var ddlAttributes = new DropDownList();
                            ddlAttributes.ID = controlId;
                            divAttribute.Controls.Add(ddlAttributes);
                            ddlAttributes.Items.Clear();

                            if (!attribute.IsRequired)
                            {
                                ddlAttributes.Items.Add(new ListItem("---", "0"));
                            }
                            var pvaValues = attribute.ProductVariantAttributeValues;

                            adjustmentTableScripts.AppendFormat("{0}['{1}'] = new Array(", AdjustmentTableName, ddlAttributes.ClientID);
                            attributeScripts.AppendFormat("$('#{0}').change(function(){{{1}();}});\n", ddlAttributes.ClientID, AdjustmentFuncName);

                            bool preSelectedSet = false;
                            foreach (var pvaValue in pvaValues)
                            {
                                string pvaValueName = pvaValue.LocalizedName;
                                if (!this.HidePrices &&
                                    (!SettingManager.GetSettingValueBoolean("Common.HidePricesForNonRegistered") ||
                                     (NopContext.Current.User != null &&
                                      !NopContext.Current.User.IsGuest)))
                                {
                                    decimal taxRate             = decimal.Zero;
                                    decimal priceAdjustmentBase = TaxManager.GetPrice(productVariant, pvaValue.PriceAdjustment, out taxRate);
                                    decimal priceAdjustment     = CurrencyManager.ConvertCurrency(priceAdjustmentBase, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                    if (priceAdjustmentBase > decimal.Zero)
                                    {
                                        pvaValueName += string.Format(" [+{0}]", PriceHelper.FormatPrice(priceAdjustment, false, false));
                                    }
                                    adjustmentTableScripts.AppendFormat(CultureInfo.InvariantCulture, "{0},", (float)priceAdjustment);
                                }
                                var pvaValueItem = new ListItem(pvaValueName, pvaValue.ProductVariantAttributeValueId.ToString());
                                if (!preSelectedSet && pvaValue.IsPreSelected)
                                {
                                    pvaValueItem.Selected = pvaValue.IsPreSelected;
                                    preSelectedSet        = true;
                                }
                                ddlAttributes.Items.Add(pvaValueItem);
                            }
                            adjustmentTableScripts.Length -= 1;
                            adjustmentTableScripts.Append(");\n");
                        }
                        break;

                        case AttributeControlTypeEnum.RadioList:
                        {
                            var rblAttributes = new RadioButtonList();
                            rblAttributes.ID = controlId;
                            divAttribute.Controls.Add(rblAttributes);
                            rblAttributes.Items.Clear();

                            var  pvaValues      = attribute.ProductVariantAttributeValues;
                            bool preSelectedSet = false;
                            foreach (var pvaValue in pvaValues)
                            {
                                string pvaValueName = pvaValue.LocalizedName;
                                if (!this.HidePrices &&
                                    (!SettingManager.GetSettingValueBoolean("Common.HidePricesForNonRegistered") ||
                                     (NopContext.Current.User != null &&
                                      !NopContext.Current.User.IsGuest)))
                                {
                                    decimal taxRate             = decimal.Zero;
                                    decimal priceAdjustmentBase = TaxManager.GetPrice(productVariant, pvaValue.PriceAdjustment, out taxRate);
                                    decimal priceAdjustment     = CurrencyManager.ConvertCurrency(priceAdjustmentBase, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                    if (priceAdjustmentBase > decimal.Zero)
                                    {
                                        pvaValueName += string.Format(" [+{0}]", PriceHelper.FormatPrice(priceAdjustment, false, false));
                                    }
                                    adjustmentTableScripts.AppendFormat(CultureInfo.InvariantCulture, "{0}['{1}_{2}'] = {3};\n", AdjustmentTableName, rblAttributes.ClientID, rblAttributes.Items.Count, (float)priceAdjustment);
                                    attributeScripts.AppendFormat("$('#{0}_{1}').click(function(){{{2}();}});\n", rblAttributes.ClientID, rblAttributes.Items.Count, AdjustmentFuncName);
                                }
                                var pvaValueItem = new ListItem(Server.HtmlEncode(pvaValueName), pvaValue.ProductVariantAttributeValueId.ToString());
                                if (!preSelectedSet && pvaValue.IsPreSelected)
                                {
                                    pvaValueItem.Selected = pvaValue.IsPreSelected;
                                    preSelectedSet        = true;
                                }
                                rblAttributes.Items.Add(pvaValueItem);
                            }
                        }
                        break;

                        case AttributeControlTypeEnum.Checkboxes:
                        {
                            var cblAttributes = new CheckBoxList();
                            cblAttributes.ID = controlId;
                            divAttribute.Controls.Add(cblAttributes);
                            cblAttributes.Items.Clear();

                            var pvaValues = attribute.ProductVariantAttributeValues;
                            foreach (var pvaValue in pvaValues)
                            {
                                string pvaValueName = pvaValue.LocalizedName;
                                if (!this.HidePrices &&
                                    (!SettingManager.GetSettingValueBoolean("Common.HidePricesForNonRegistered") ||
                                     (NopContext.Current.User != null &&
                                      !NopContext.Current.User.IsGuest)))
                                {
                                    decimal taxRate             = decimal.Zero;
                                    decimal priceAdjustmentBase = TaxManager.GetPrice(productVariant, pvaValue.PriceAdjustment, out taxRate);
                                    decimal priceAdjustment     = CurrencyManager.ConvertCurrency(priceAdjustmentBase, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                    if (priceAdjustmentBase > decimal.Zero)
                                    {
                                        pvaValueName += string.Format(" [+{0}]", PriceHelper.FormatPrice(priceAdjustment, false, false));
                                    }
                                    adjustmentTableScripts.AppendFormat(CultureInfo.InvariantCulture, "{0}['{1}_{2}'] = {3};\n", AdjustmentTableName, cblAttributes.ClientID, cblAttributes.Items.Count, (float)priceAdjustment);
                                    attributeScripts.AppendFormat("$('#{0}_{1}').click(function(){{{2}();}});\n", cblAttributes.ClientID, cblAttributes.Items.Count, AdjustmentFuncName);
                                }
                                var pvaValueItem = new ListItem(Server.HtmlEncode(pvaValueName), pvaValue.ProductVariantAttributeValueId.ToString());
                                pvaValueItem.Selected = pvaValue.IsPreSelected;
                                cblAttributes.Items.Add(pvaValueItem);
                            }
                        }
                        break;

                        case AttributeControlTypeEnum.TextBox:
                        {
                            var txtAttribute = new TextBox();
                            txtAttribute.Width = SettingManager.GetSettingValueInteger("ProductAttribute.Textbox.Width", 300);
                            txtAttribute.ID    = controlId;
                            divAttribute.Controls.Add(txtAttribute);
                        }
                        break;

                        case AttributeControlTypeEnum.MultilineTextbox:
                        {
                            var txtAttribute = new TextBox();
                            txtAttribute.ID       = controlId;
                            txtAttribute.TextMode = TextBoxMode.MultiLine;
                            txtAttribute.Width    = SettingManager.GetSettingValueInteger("ProductAttribute.MultiTextbox.Width", 300);
                            txtAttribute.Height   = SettingManager.GetSettingValueInteger("ProductAttribute.MultiTextbox.Height", 150);
                            divAttribute.Controls.Add(txtAttribute);
                        }
                        break;

                        case AttributeControlTypeEnum.Datepicker:
                        {
                            var datePicker = new NopDatePicker();
                            //changes these properties in order to change year range
                            datePicker.FirstYear = DateTime.Now.Year;
                            datePicker.LastYear  = DateTime.Now.Year + 1;
                            datePicker.ID        = controlId;
                            divAttribute.Controls.Add(datePicker);
                        }
                        break;

                        default:
                            break;
                        }
                    }

                    lblAdjustmentTableScripts.Text = adjustmentTableScripts.ToString();
                    lblAttributeScripts.Text       = attributeScripts.ToString();
                }
                else
                {
                    this.Visible = false;
                }
            }
            else
            {
                this.Visible = false;
            }
        }