Skip to content

Commit

Permalink
Product details - allow select only existing attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Sep 8, 2021
1 parent c15988d commit f9b2ebe
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Web/Grand.Web/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public virtual async Task<IActionResult> ProductDetails_AttributeChange(string p
buttonTextOutOfStockSubscription = model.ButtonTextOutOfStockSubscription,
enabledattributemappingids = model.EnabledAttributeMappingIds.ToArray(),
disabledattributemappingids = model.DisabledAttributeMappingids.ToArray(),
notAvailableAttributeMappingids = model.NotAvailableAttributeMappingids.ToArray(),
pictureFullSizeUrl = model.PictureFullSizeUrl,
pictureDefaultSizeUrl = model.PictureDefaultSizeUrl,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand Down Expand Up @@ -141,7 +142,28 @@ public async Task<ProductDetailsAttributeChangeModel> Handle(GetProductDetailsAt
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)
Expand Down Expand Up @@ -173,8 +195,7 @@ public async Task<ProductDetailsAttributeChangeModel> Handle(GetProductDetailsAt

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public ProductDetailsAttributeChangeModel()
{
EnabledAttributeMappingIds = new List<string>();
DisabledAttributeMappingids = new List<string>();
NotAvailableAttributeMappingids = new List<string>();
}
public string Gtin { get; set; }
public string Mpn { get; set; }
Expand All @@ -19,6 +20,7 @@ public ProductDetailsAttributeChangeModel()
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; }

Expand Down
25 changes: 20 additions & 5 deletions src/Web/Grand.Web/Views/Product/_ProductAttributes.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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>
Expand All @@ -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">
Expand Down Expand Up @@ -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">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f9b2ebe

Please sign in to comment.