Skip to content

Commit 8c81200

Browse files
Stock of product was shown when an attribute for an attribute combination was selected
1 parent e2e86b3 commit 8c81200

5 files changed

Lines changed: 143 additions & 58 deletions

File tree

src/Libraries/SmartStore.Services/Orders/IShoppingCartService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ List<string> AddToCart(Customer customer, Product product, ShoppingCartType cart
170170
/// <param name="ctx">Add to cart context</param>
171171
void AddToCartStoring(AddToCartContext ctx);
172172

173+
/// <summary>
174+
/// Validates if all required attributes are selected
175+
/// </summary>
176+
/// <param name="selectedAttributes">Selected attributes</param>
177+
/// <param name="product">Product</param>
178+
/// <returns>bool</returns>
179+
bool AreAllAttributesForCombinationSelected(string selectedAttributes, Product product);
180+
173181
/// <summary>
174182
/// Updates the shopping cart item
175183
/// </summary>

src/Libraries/SmartStore.Services/Orders/ShoppingCartService.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,58 @@ public virtual IList<string> GetShoppingCartItemAttributeWarnings(Customer custo
591591
return warnings;
592592
}
593593

594+
/// <summary>
595+
/// Validates if all required attributes are selected
596+
/// </summary>
597+
/// <param name="selectedAttributes">Selected attributes</param>
598+
/// <param name="product">Product</param>
599+
/// <returns>bool</returns>
600+
public virtual bool AreAllAttributesForCombinationSelected(string selectedAttributes, Product product)
601+
{
602+
if (product.ProductVariantAttributeCombinations.Count == 0)
603+
return true;
604+
605+
//selected attributes
606+
var pva1Collection = _productAttributeParser.ParseProductVariantAttributes(selectedAttributes);
607+
608+
//existing product attributes
609+
var pva2Collection = product.ProductVariantAttributes;
610+
foreach (var pva2 in pva2Collection)
611+
{
612+
if (pva2.IsRequired)
613+
{
614+
bool found = false;
615+
//selected product attributes
616+
foreach (var pva1 in pva1Collection)
617+
{
618+
if (pva1.Id == pva2.Id)
619+
{
620+
var pvaValuesStr = _productAttributeParser.ParseValues(selectedAttributes, pva1.Id);
621+
foreach (string str1 in pvaValuesStr)
622+
{
623+
if (!String.IsNullOrEmpty(str1.Trim()))
624+
{
625+
found = true;
626+
break;
627+
}
628+
}
629+
}
630+
}
631+
632+
if (!found)
633+
{
634+
return found;
635+
}
636+
}
637+
else
638+
{
639+
return true;
640+
}
641+
}
642+
643+
return true;
644+
}
645+
594646
/// <summary>
595647
/// Validates shopping cart item (gift card)
596648
/// </summary>

src/Presentation/SmartStore.Web/Controllers/CatalogHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ public ProductDetailsModel PrepareProductDetailModel(
628628
{
629629
// cases where stock inventory is not functional. determined by what ShoppingCartService.GetStandardWarnings and ProductService.AdjustInventory is not handling.
630630
model.IsAvailable = true;
631-
model.StockAvailability = "";
631+
model.StockAvailability = product.ProductVariantAttributeCombinations.Count == 0 ? product.FormatStockMessage(_localizationService) : "";
632632
}
633633
else if (model.IsAvailable)
634634
{

src/Presentation/SmartStore.Web/Controllers/ProductController.cs

Lines changed: 73 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public partial class ProductController : PublicControllerBase
5959
private readonly LocalizationSettings _localizationSettings;
6060
private readonly CaptchaSettings _captchaSettings;
6161
private readonly CatalogHelper _helper;
62+
private readonly IDownloadService _downloadService;
63+
private readonly ILocalizationService _localizationService;
6264

6365
#endregion
6466

@@ -90,7 +92,9 @@ public ProductController(
9092
ShoppingCartSettings shoppingCartSettings,
9193
LocalizationSettings localizationSettings,
9294
CaptchaSettings captchaSettings,
93-
CatalogHelper helper)
95+
CatalogHelper helper,
96+
IDownloadService downloadService,
97+
ILocalizationService localizationService)
9498
{
9599
this._services = services;
96100
this._manufacturerService = manufacturerService;
@@ -118,6 +122,8 @@ public ProductController(
118122
this._localizationSettings = localizationSettings;
119123
this._captchaSettings = captchaSettings;
120124
this._helper = helper;
125+
this._downloadService = downloadService;
126+
this._localizationService = localizationService;
121127

122128
T = NullLocalizer.Instance;
123129
}
@@ -662,62 +668,73 @@ public ActionResult UpdateProductDetails(int productId, string itemType, int bun
662668
}
663669
}
664670

671+
var attributes = _productAttributeService.GetProductVariantAttributesByProductId(productId);
672+
var warnings = new List<string>();
673+
string attributeXml = form.CreateSelectedAttributesXml(productId, attributes, _productAttributeParser,
674+
_localizationService, _downloadService, _catalogSettings, this.Request, warnings, true);
675+
665676
#region data object
666-
object data = new
667-
{
668-
Delivery = new
669-
{
670-
Id = 0,
671-
Name = m.DeliveryTimeName,
672-
Color = m.DeliveryTimeHexValue,
673-
DisplayAccordingToStock = m.DisplayDeliveryTimeAccordingToStock
674-
},
675-
Measure = new
676-
{
677-
Weight = new { Value = m.WeightValue, Text = m.Weight },
678-
Height = new { Value = product.Height, Text = m.Height },
679-
Width = new { Value = product.Width, Text = m.Width },
680-
Length = new { Value = product.Length, Text = m.Length }
681-
},
682-
Number = new
683-
{
684-
Sku = new { Value = m.Sku, Show = m.ShowSku },
685-
Gtin = new { Value = m.Gtin, Show = m.ShowGtin },
686-
Mpn = new { Value = m.ManufacturerPartNumber, Show = m.ShowManufacturerPartNumber }
687-
},
688-
Price = new
689-
{
690-
Base = new
691-
{
692-
Enabled = m.IsBasePriceEnabled,
693-
Info = m.BasePriceInfo
694-
},
695-
Old = new
696-
{
697-
Value = decimal.Zero,
698-
Text = m.ProductPrice.OldPrice
699-
},
700-
WithoutDiscount = new
701-
{
702-
Value = m.ProductPrice.PriceValue,
703-
Text = m.ProductPrice.Price
704-
},
705-
WithDiscount = new
706-
{
707-
Value = m.ProductPrice.PriceWithDiscountValue,
708-
Text = m.ProductPrice.PriceWithDiscount
709-
}
710-
},
711-
Stock = new
712-
{
713-
Quantity = new { Value = product.StockQuantity, Show = product.DisplayStockQuantity },
714-
Availability = new { Text = m.StockAvailability, Show = product.DisplayStockAvailability, Available = m.IsAvailable }
715-
},
716-
717-
DynamicThumblUrl = dynamicThumbUrl,
718-
GalleryStartIndex = galleryStartIndex,
719-
GalleryHtml = galleryHtml
720-
};
677+
object data = new
678+
{
679+
Delivery = new
680+
{
681+
Id = 0,
682+
Name = m.DeliveryTimeName,
683+
Color = m.DeliveryTimeHexValue,
684+
DisplayAccordingToStock = m.DisplayDeliveryTimeAccordingToStock
685+
},
686+
Measure = new
687+
{
688+
Weight = new { Value = m.WeightValue, Text = m.Weight },
689+
Height = new { Value = product.Height, Text = m.Height },
690+
Width = new { Value = product.Width, Text = m.Width },
691+
Length = new { Value = product.Length, Text = m.Length }
692+
},
693+
Number = new
694+
{
695+
Sku = new { Value = m.Sku, Show = m.ShowSku },
696+
Gtin = new { Value = m.Gtin, Show = m.ShowGtin },
697+
Mpn = new { Value = m.ManufacturerPartNumber, Show = m.ShowManufacturerPartNumber }
698+
},
699+
Price = new
700+
{
701+
Base = new
702+
{
703+
Enabled = m.IsBasePriceEnabled,
704+
Info = m.BasePriceInfo
705+
},
706+
Old = new
707+
{
708+
Value = decimal.Zero,
709+
Text = m.ProductPrice.OldPrice
710+
},
711+
WithoutDiscount = new
712+
{
713+
Value = m.ProductPrice.PriceValue,
714+
Text = m.ProductPrice.Price
715+
},
716+
WithDiscount = new
717+
{
718+
Value = m.ProductPrice.PriceWithDiscountValue,
719+
Text = m.ProductPrice.PriceWithDiscount
720+
}
721+
},
722+
Stock = new
723+
{
724+
Quantity = new {
725+
Value = product.StockQuantity,
726+
Show = _shoppingCartService.AreAllAttributesForCombinationSelected(attributeXml, product) ? product.DisplayStockQuantity : false
727+
},
728+
Availability = new {
729+
Text = m.StockAvailability,
730+
Show = _shoppingCartService.AreAllAttributesForCombinationSelected(attributeXml, product) ? product.DisplayStockAvailability : false,
731+
Available = m.IsAvailable }
732+
},
733+
734+
DynamicThumblUrl = dynamicThumbUrl,
735+
GalleryStartIndex = galleryStartIndex,
736+
GalleryHtml = galleryHtml
737+
};
721738
#endregion
722739

723740
return new JsonResult { Data = data };

src/Presentation/SmartStore.Web/Scripts/public.product-detail.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,15 @@
130130
updateAttrLine(".attr-length", data.Measure.Length.Text);
131131
updateAttrLine(".attr-width", data.Measure.Width.Text);
132132
updateAttrLine(".attr-height", data.Measure.Height.Text);
133-
updateAttrLine(".attr-stock", data.Stock.Availability.Text);
133+
134+
if (data.Stock.Quantity.Show)
135+
{
136+
updateAttrLine(".attr-stock", data.Stock.Availability.Text);
137+
}
138+
else
139+
{
140+
updateAttrLine(".attr-stock", "");
141+
}
134142

135143
context.find('.add-to-cart .form-inline').toggle(data.Stock.Availability.Available);
136144

0 commit comments

Comments
 (0)