Hi All,
When you are managing stock by attribute combinations - is there a way to only display attribute combinations that are in stock or allow out of stock orders? Or will I need to write some custom code?
Thanks
Jacques.
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();
List<ProductVariantAttributeCombination> pVAttributeCombonations = null;
if (attribute.IsRequired)
attributeTitle.Text = "<span>*</span> ";
if (productVariant.ManageInventory == 2)
{
pVAttributeCombonations = ProductAttributeManager.GetAllProductVariantAttributeCombinations(productVariant.ProductVariantId);
}
//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 += " ";
}
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);
}
if (productVariantAttributes.Count == 1 && productVariant.ManageInventory == 2)
{
//Only one attibute and we're tracking by attribute combinations, so display the stock qty's
foreach (var pvaCombo in pVAttributeCombonations)
{
if (ProductAttributeHelper.AreProductAttributesEqual(pvaCombo.AttributesXml, ProductAttributeHelper.AddProductAttribute("", attribute, pvaValue.ProductVariantAttributeValueId.ToString())))
{
if (pvaCombo.StockQuantity == 0)
{
pvaValueName += " [" + LocalizationManager.GetLocaleResourceString("Products.OutOfStock") + "]";
}
else
{
if (productVariant.DisplayStockQuantity)
{
//display "in stock" with stock quantity
pvaValueName += string.Format(" [" + string.Format(LocalizationManager.GetLocaleResourceString("Products.InStockWithQuantity"), pvaCombo.StockQuantity) + "]", pvaCombo.StockQuantity);
}
else
{
//display "in stock" without stock quantity
pvaValueName += " [" + LocalizationManager.GetLocaleResourceString("Products.InStock") + "]";
}
}
}
}
}
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);
}
if (productVariantAttributes.Count == 1 && productVariant.ManageInventory == 2)
{
//Only one attibute and we're tracking by attribute combinations, so display the stock qty's
foreach (var pvaCombo in pVAttributeCombonations)
{
if (ProductAttributeHelper.AreProductAttributesEqual(pvaCombo.AttributesXml, ProductAttributeHelper.AddProductAttribute("", attribute, pvaValue.ProductVariantAttributeValueId.ToString())))
{
if (pvaCombo.StockQuantity == 0)
{
pvaValueName += " [" + LocalizationManager.GetLocaleResourceString("Products.OutOfStock") + "]";
}
else
{
if (productVariant.DisplayStockQuantity)
{
//display "in stock" with stock quantity
pvaValueName += string.Format(" [" + string.Format(LocalizationManager.GetLocaleResourceString("Products.InStockWithQuantity"), pvaCombo.StockQuantity) + "]", pvaCombo.StockQuantity);
}
else
{
//display "in stock" without stock quantity
pvaValueName += " [" + LocalizationManager.GetLocaleResourceString("Products.InStock") + "]";
}
}
}
}
}
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);
}
if (productVariantAttributes.Count == 1 && productVariant.ManageInventory == 2)
{
//Only one attibute and we're tracking by attribute combinations, so display the stock qty's
foreach (var pvaCombo in pVAttributeCombonations)
{
if (ProductAttributeHelper.AreProductAttributesEqual(pvaCombo.AttributesXml, ProductAttributeHelper.AddProductAttribute("", attribute, pvaValue.ProductVariantAttributeValueId.ToString())))
{
if (pvaCombo.StockQuantity == 0)
{
pvaValueName += " [" + LocalizationManager.GetLocaleResourceString("Products.OutOfStock") + "]";
}
else
{
if (productVariant.DisplayStockQuantity)
{
//display "in stock" with stock quantity
pvaValueName += string.Format(" [" + string.Format(LocalizationManager.GetLocaleResourceString("Products.InStockWithQuantity"), pvaCombo.StockQuantity) + "]", pvaCombo.StockQuantity);
}
else
{
//display "in stock" without stock quantity
pvaValueName += " [" + LocalizationManager.GetLocaleResourceString("Products.InStock") + "]";
}
}
}
}
}
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;
}
}