Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Product details - allow select only existing attributes

KrzysztofPajakf9b2ebe619eb
diff --git a/src/Web/Grand.Web/Controllers/ProductController.cs b/src/Web/Grand.Web/Controllers/ProductController.cs
index 6a984f028..9a0dae5b2 100644
--- a/src/Web/Grand.Web/Controllers/ProductController.cs
+++ b/src/Web/Grand.Web/Controllers/ProductController.cs
@@ -213,6 +213,7 @@ namespace Grand.Web.Controllers
buttonTextOutOfStockSubscription = model.ButtonTextOutOfStockSubscription,
enabledattributemappingids = model.EnabledAttributeMappingIds.ToArray(),
disabledattributemappingids = model.DisabledAttributeMappingids.ToArray(),
+ notAvailableAttributeMappingids = model.NotAvailableAttributeMappingids.ToArray(),
pictureFullSizeUrl = model.PictureFullSizeUrl,
pictureDefaultSizeUrl = model.PictureDefaultSizeUrl,
});
diff --git a/src/Web/Grand.Web/Features/Handlers/Products/GetProductDetailsAttributeChangeHandler.cs b/src/Web/Grand.Web/Features/Handlers/Products/GetProductDetailsAttributeChangeHandler.cs
index 245b67b0f..a22b0b531 100644
--- a/src/Web/Grand.Web/Features/Handlers/Products/GetProductDetailsAttributeChangeHandler.cs
+++ b/src/Web/Grand.Web/Features/Handlers/Products/GetProductDetailsAttributeChangeHandler.cs
@@ -11,6 +11,7 @@ using Grand.Domain.Catalog;
using Grand.Domain.Common;
using Grand.Domain.Media;
using Grand.Domain.Orders;
+using Grand.SharedKernel.Extensions;
using Grand.Web.Extensions;
using Grand.Web.Features.Models.Products;
using Grand.Web.Features.Models.ShoppingCart;
@@ -141,7 +142,28 @@ namespace Grand.Web.Features.Handlers.Products
model.ButtonTextOutOfStockSubscription = _translationService.GetResource("OutOfStockSubscriptions.NotifyMeWhenAvailable");
}
+ if (request.Product.ManageInventoryMethodId == ManageInventoryMethod.ManageStockByAttributes)
+ {
+ var combination = _productAttributeParser.FindProductAttributeCombination(request.Product, customAttributes);
+ foreach (var customAttribute in customAttributes)
+ {
+ //find all combinations with attributes
+ var combinations = request.Product.ProductAttributeCombinations
+ .Where(x => x.Attributes.Any(z => z.Key == customAttribute.Key && z.Value == customAttribute.Value)).ToList();
+ //limit to without existing combination
+ combinations = combinations.Where(x => x.Id != combination?.Id).ToList();
+ //where the stock is unavailable
+ combinations = combinations.Where(x => x.StockQuantity - x.ReservedQuantity <= 0).ToList();
+
+ if (combinations.Count > 0)
+ {
+ var x = combinations.SelectMany(x => x.Attributes).Where(x => x.Value != customAttribute.Value);
+ var notAvailable = x.Select(x => x.Value).Distinct().ToList();
+ notAvailable.ForEach((x) => model.NotAvailableAttributeMappingids.Add(x));
+ }
+ }
+ }
//conditional attributes
if (request.ValidateAttributeConditions)
@@ -173,8 +195,7 @@ namespace Grand.Web.Features.Handlers.Products
if (!string.IsNullOrEmpty(pictureId))
{
- var pictureModel = new PictureModel
- {
+ var pictureModel = new PictureModel {
Id = pictureId,
FullSizeImageUrl = await _pictureService.GetPictureUrl(pictureId),
ImageUrl = await _pictureService.GetPictureUrl(pictureId, _mediaSettings.ProductDetailsPictureSize)
diff --git a/src/Web/Grand.Web/Models/Catalog/ProductDetailsAttributeChangeModel.cs b/src/Web/Grand.Web/Models/Catalog/ProductDetailsAttributeChangeModel.cs
index 69c38aa85..f6c53c520 100644
--- a/src/Web/Grand.Web/Models/Catalog/ProductDetailsAttributeChangeModel.cs
+++ b/src/Web/Grand.Web/Models/Catalog/ProductDetailsAttributeChangeModel.cs
@@ -9,6 +9,7 @@ namespace Grand.Web.Models.Catalog
{
EnabledAttributeMappingIds = new List<string>();
DisabledAttributeMappingids = new List<string>();
+ NotAvailableAttributeMappingids = new List<string>();
}
public string Gtin { get; set; }
public string Mpn { get; set; }
@@ -19,6 +20,7 @@ namespace Grand.Web.Models.Catalog
public string ButtonTextOutOfStockSubscription { get; set; }
public IList<string> EnabledAttributeMappingIds { get; set; }
public IList<string> DisabledAttributeMappingids { get; set; }
+ public IList<string> NotAvailableAttributeMappingids { get; set; }
public string PictureFullSizeUrl { get; set; }
public string PictureDefaultSizeUrl { get; set; }
diff --git a/src/Web/Grand.Web/Views/Product/_ProductAttributes.cshtml b/src/Web/Grand.Web/Views/Product/_ProductAttributes.cshtml
index b5c3317a5..0b4aa0936 100644
--- a/src/Web/Grand.Web/Views/Product/_ProductAttributes.cshtml
+++ b/src/Web/Grand.Web/Views/Product/_ProductAttributes.cshtml
@@ -59,16 +59,20 @@
document.querySelector('.price-value-@productId').innerText = response.data.price;
}
if (response.data.sku) {
- document.querySelector('#sku-@productId').innerText = response.data.sku;
+ if(document.querySelector('#sku-@productId'))
+ document.querySelector('#sku-@productId').innerText = response.data.sku;
}
if (response.data.mpn) {
- document.querySelector('#mpn-@productId').innerText = response.data.mpn;
+ if(document.querySelector('#mpn-@productId'))
+ document.querySelector('#mpn-@productId').innerText = response.data.mpn;
}
if (response.data.gtin) {
- document.querySelector('#gtin-@productId').innerText = response.data.gtin;
+ if(document.querySelector('#gtin-@productId'))
+ document.querySelector('#gtin-@productId').innerText = response.data.gtin;
}
if (response.data.stockAvailability) {
- document.querySelector('#stock-availability-value-@productId').innerText = response.data.stockAvailability;
+ if(document.querySelector('#stock-availability-value-@productId'))
+ document.querySelector('#stock-availability-value-@productId').innerText = response.data.stockAvailability;
}
if (response.data.outOfStockSubscription) {
var element = document.querySelector('#out-of-stock-subscribe-@productId');
@@ -98,6 +102,15 @@
document.querySelector('#product_attribute_input_' + response.data.disabledattributemappingids[i]).style.display = "none";
}
}
+ if (response.data.notAvailableAttributeMappingids) {
+ document.querySelectorAll('[data-disable]').forEach((element) => element.disabled = false);
+ for (var i = 0; i < response.data.notAvailableAttributeMappingids.length; i++) {
+ if (document.querySelectorAll("[data-disable='" + response.data.notAvailableAttributeMappingids[i] + "']").length > 0) {
+ document.querySelectorAll("[data-disable='" + response.data.notAvailableAttributeMappingids[i] + "']")[0].disabled = true;
+ }
+ }
+ }
+
var product = this.product;
setTimeout(function () {
if (response.data.pictureDefaultSizeUrl) {
@@ -172,7 +185,7 @@
<label :for="'product_attribute_' + attribute.Id" class="sr-only">product_attribute_{{attribute.Id}}</label>
<select :id="'product_attribute_' + attribute.Id" class="form-control custom-select" :name="'product_attribute_' + attribute.Id">
<template v-if="attribute.IsRequired==false"> <option value="">---</option> </template>
- <option v-for="attributeValue in attribute.Values" :selected="attributeValue.IsPreSelected" :value="attributeValue.Id">
+ <option v-for="attributeValue in attribute.Values" :selected="attributeValue.IsPreSelected" :value="attributeValue.Id" :data-disable="attributeValue.Id">
<template v-if="attributeValue.PriceAdjustment==null">{{attributeValue.Name}}</template> <template v-else>{{attributeValue.Name}}[{{attributeValue.PriceAdjustment}}] </template>
</option>
</select>
@@ -190,6 +203,7 @@
:name="'product_attribute_' + attribute.Id"
:value="attributeValue.Id"
:checked="attributeValue.IsPreSelected"
+ :data-disable="attributeValue.Id"
/>
<span class="custom-control-label"></span>
<span class="custom-control-description" :for="'product_attribute_' + attribute.Id + '_' + attributeValue.Id">
@@ -224,6 +238,7 @@
:name="'product_attribute_' + attribute.Id"
:value="attributeValue.Id"
:checked="attributeValue.IsPreSelected"
+ :data-disable="attributeValue.Id"
/>
<span class="custom-control-label"></span>
<span class="custom-control-description" :for="'product_attribute_' + attribute.Id + '_' + attributeValue.Id">
diff --git a/src/Web/Grand.Web/Views/Product/_ProductAttributesBundle.cshtml b/src/Web/Grand.Web/Views/Product/_ProductAttributesBundle.cshtml
index fb88b3d04..f9af3e6e7 100644
--- a/src/Web/Grand.Web/Views/Product/_ProductAttributesBundle.cshtml
+++ b/src/Web/Grand.Web/Views/Product/_ProductAttributesBundle.cshtml
@@ -59,13 +59,16 @@
document.querySelector('.price-value-@productId').innerText = response.data.price;
}
if (response.data.sku) {
- document.querySelector('#sku-@productId').innerText = response.data.sku;
+ if(document.querySelector('#sku-@productId'))
+ document.querySelector('#sku-@productId').innerText = response.data.sku;
}
if (response.data.mpn) {
- document.querySelector('#mpn-@productId').innerText = response.data.mpn;
+ if(document.querySelector('#mpn-@productId'))
+ document.querySelector('#mpn-@productId').innerText = response.data.mpn;
}
if (response.data.gtin) {
- document.querySelector('#gtin-@productId').innerText = response.data.gtin;
+ if(document.querySelector('#gtin-@productId'))
+ document.querySelector('#gtin-@productId').innerText = response.data.gtin;
}
if (response.data.stockAvailability) {
document.querySelector('#stock-availability-value-@productId').innerText = response.data.stockAvailability;