protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // delete
                int?deleteId = WA.Parser.ToInt(Request.QueryString["delete"]);
                if (deleteId.HasValue)
                {
                    ProductField toDelete = new ProductField();
                    if (toDelete.LoadByPrimaryKey(deleteId.Value))
                    {
                        int productId = toDelete.ProductId.Value;
                        toDelete.MarkAsDeleted();
                        toDelete.Save();

                        Response.Redirect(StoreUrls.AdminEditProduct(productId, "Product variant/attribute deleted"));
                    }
                }

                PopulateListControls();
                if (productField.Id.HasValue)
                {
                    FillForEdit();
                }
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            // edit
            int?id = ParamId;

            if (id.HasValue && productField.LoadByPrimaryKey(id.Value))
            {
                product.LoadByPrimaryKey(productField.ProductId.Value);
            }
            else
            {
                if (ParamProductId.HasValue)
                {
                    product.LoadByPrimaryKey(ParamProductId.Value);
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ProductField toSave = new ProductField();

            if (!toSave.LoadByPrimaryKey(ParamId.GetValueOrDefault(-1)))
            {
                // NEW field
                toSave           = new ProductField();
                toSave.ProductId = ParamProductId.Value;
                ProductFieldWidgetType widgetType;
                if (!WA.Enum <ProductFieldWidgetType> .TryParse(ddlWidgetType.SelectedValue, out widgetType))
                {
                    throw new ArgumentException("Unable to determine WidgetType");
                }
                toSave.WidgetType = ddlWidgetType.SelectedValue;
                toSave.SortOrder  = 99;
                toSave.Slug       = txtName.Text.CreateSpecialSlug(true, 100);
                if (ProductField.SlugExistsForProductField(toSave.ProductId.Value, toSave.Slug))
                {
                    toSave.Slug = ProductField.GetNextAvailableSlug(toSave.ProductId.Value, toSave.Slug);
                }
            }

            toSave.Name       = txtName.Text;
            toSave.IsRequired = chkIsRequired.Checked;

            switch (toSave.WidgetTypeName)
            {
            case ProductFieldWidgetType.Textbox:
            case ProductFieldWidgetType.Textarea:
            case ProductFieldWidgetType.Checkbox:
                toSave.PriceAdjustment  = WA.Parser.ToDecimal(txtPriceAdjustment.Text);
                toSave.WeightAdjustment = WA.Parser.ToDecimal(txtWeightAdjustment.Text);

                // no 'choices' for these types
                toSave.ProductFieldChoiceCollectionByProductFieldId.MarkAllAsDeleted();
                break;

            default:
                // no field-level adjustments for these types (adjustments are done at the choice-level)
                toSave.PriceAdjustment  = null;
                toSave.WeightAdjustment = null;

                // save/update the 'choices'
                List <string> optionNames         = WA.Web.WebHelper.GetFormValuesByName("optionName", Request);
                List <string> optionValues        = WA.Web.WebHelper.GetFormValuesByName("optionValue", Request);
                List <string> optionPriceAdjusts  = WA.Web.WebHelper.GetFormValuesByName("optionPriceAdjust", Request);
                List <string> optionWeightAdjusts = WA.Web.WebHelper.GetFormValuesByName("optionWeightAdjust", Request);
                if (optionNames.Count == optionPriceAdjusts.Count && optionNames.Count == optionWeightAdjusts.Count)
                {
                    toSave.ProductFieldChoiceCollectionByProductFieldId.MarkAllAsDeleted();

                    for (int i = 0; i < optionNames.Count; i++)
                    {
                        if (!string.IsNullOrEmpty(optionNames[i]))
                        {
                            ProductFieldChoice newChoice = toSave.ProductFieldChoiceCollectionByProductFieldId.AddNew();
                            newChoice.Name             = optionNames[i];
                            newChoice.Value            = string.IsNullOrEmpty(optionValues[i]) ? optionNames[i] : optionValues[i];
                            newChoice.PriceAdjustment  = WA.Parser.ToDecimal(optionPriceAdjusts[i]);
                            newChoice.WeightAdjustment = WA.Parser.ToDecimal(optionWeightAdjusts[i]);
                            newChoice.SortOrder        = (short)i;
                        }
                    }
                }
                else
                {
                    throw new ArgumentException(
                              "Unable to parse/determine values for Product Choice Names and Adjustments");
                }
                break;
            }
            toSave.Save();


            // redirect back to Edit Product Url
            Response.Redirect(StoreUrls.AdminEditProduct(toSave.ProductId.Value, "Product variant/attribute saved"));
        }
Exemple #4
0
        private List <JsonProductFieldData> GetPostedWidgetValues(HttpRequest request)
        {
            List <JsonProductFieldData> fieldData = new List <JsonProductFieldData>();

            NameValueCollection postedForm = request.Form;

            foreach (string key in postedForm.Keys)
            {
                Match m = rxInputName.Match(key);
                if (m.Success && m.Groups.Count > 1)
                {
                    int?productFieldId = WA.Parser.ToInt(m.Groups[1].Value);

                    string postedValue = postedForm.Get(key);
                    if (!string.IsNullOrEmpty(postedValue))
                    {
                        ProductField productField = new ProductField();
                        if (productField.LoadByPrimaryKey(productFieldId.GetValueOrDefault(-1)))
                        {
                            decimal priceAdjust  = 0;
                            decimal weightAdjust = 0;

                            List <string> postedChoiceValues = new List <string>(postedForm.GetValues(key) ?? new string[] { });
                            // we need to special-case the "Checkbox" type and provide a better "choice value" than "on" (browser default)
                            if (productField.WidgetTypeName == ProductFieldWidgetType.Checkbox)
                            {
                                postedChoiceValues = new List <string>()
                                {
                                    "Yes"
                                };
                            }

                            List <ProductFieldChoice> fieldChoices = productField.GetChoicesInSortOrder().ToList();
                            if (fieldChoices.Count > 0)
                            {
                                // widget has choices
                                fieldChoices.RemoveAll(c => !postedChoiceValues.Contains(c.Value));
                                foreach (ProductFieldChoice fieldChoice in fieldChoices)
                                {
                                    priceAdjust  += fieldChoice.PriceAdjustment.GetValueOrDefault(0);
                                    weightAdjust += fieldChoice.WeightAdjustment.GetValueOrDefault(0);
                                }
                            }
                            else
                            {
                                priceAdjust  = productField.PriceAdjustment.GetValueOrDefault(0);
                                weightAdjust = productField.WeightAdjustment.GetValueOrDefault(0);
                            }

                            JsonProductFieldData jsonProductField = new JsonProductFieldData()
                            {
                                ProductFieldId   = productField.Id.Value,
                                Name             = productField.Name,
                                Slug             = productField.Slug,
                                PriceAdjustment  = priceAdjust,
                                WeightAdjustment = weightAdjust,
                                ChoiceValues     = postedChoiceValues
                            };
                            fieldData.Add(jsonProductField);
                        }
                    }
                }
            }

            return(fieldData);
        }