-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OCC-163: PriceVariantsPart doesn't support InventoryPart #315
Merged
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
18560c0
Working on reworking Inventory (moderately messy)
porgabi 1d8b48d
Adding method to fake interface
porgabi e5b8d1c
Adding InventoryPart to Price Variant Product
porgabi ce66045
Adapting inventory management methods to dictionary inventories
porgabi 6a53547
Adapting product availability displays to dictionary inventory
porgabi d6add07
Hiding unavailable Price Variant Products items
porgabi d93d52b
Handling item quantity verification
porgabi 71a3330
Using new method
porgabi 41b4007
Renaming property
porgabi 9a22dd6
Handling out of stock message
porgabi 24cc5c8
Adding editor for inventory values
porgabi 43dde59
Updating inventory keys upon SKU change
porgabi db3162a
Updating Product's inventory to its SKU from "DEFAULT"
porgabi 7dae3f8
Code styling adjustments
porgabi 7dad30f
Also updating CanBeBought entry keys
porgabi ba50896
WIP something
porgabi 382fc9b
Working on reworking inventory keys updating
porgabi 8402424
Trying to resolve bug with dictionaries
porgabi 9c2b900
Fixing user-facing variant availability display
porgabi b365173
Merge remote-tracking branch 'origin/main' into issue/OCC-163
porgabi eb80b01
Same
porgabi 632c767
Removing
porgabi 558ba25
Adding StringComparer to view model's property
porgabi 8333b66
Finding workaround for giga dictionaries bug
porgabi 86baf4c
Removing unnecessary code
porgabi 9433356
Initializing new inventory when creating a new Product item
porgabi e0c267e
Eliminating small bug with workaround
porgabi d5eb05d
Completing workaround
porgabi 3ef9267
Applying filtering in other driver as well
porgabi b7a9f81
Initializing new inventories in drivers
porgabi 2468cf6
Removing unnecessary placement
porgabi b6ceb2a
Reverting
porgabi ab5c45f
Fixing out of stock message display conditions
porgabi c1b2150
Fixing submit button clickability
porgabi cda7ca1
Donating brain cell to spell-checking
porgabi 39aef9e
Adding minor improvements
porgabi a9ecbf7
Adding inventory filtering extension method
porgabi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/Modules/OrchardCore.Commerce.Inventory/Drivers/InventoryPartDisplayDriver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using OrchardCore.Commerce.Inventory.Models; | ||
using OrchardCore.Commerce.Inventory.ViewModels; | ||
using OrchardCore.ContentManagement.Display.ContentDisplay; | ||
using OrchardCore.ContentManagement.Display.Models; | ||
using OrchardCore.DisplayManagement.ModelBinding; | ||
using OrchardCore.DisplayManagement.Views; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace OrchardCore.Commerce.Inventory.Drivers; | ||
|
||
public class InventoryPartDisplayDriver : ContentPartDisplayDriver<InventoryPart> | ||
{ | ||
private readonly IHttpContextAccessor _hca; | ||
|
||
public InventoryPartDisplayDriver(IHttpContextAccessor hca) => _hca = hca; | ||
|
||
public override IDisplayResult Display(InventoryPart part, BuildPartDisplayContext context) => | ||
Initialize<InventoryPartViewModel>(GetDisplayShapeType(context), viewModel => BuildViewModel(viewModel, part)) | ||
.Location("Detail", "Content:26") | ||
.Location("Summary", "Meta"); | ||
|
||
public override IDisplayResult Edit(InventoryPart part, BuildPartEditorContext context) => | ||
Initialize<InventoryPartViewModel>(GetEditorShapeType(context), viewModel => BuildViewModel(viewModel, part)); | ||
|
||
public override async Task<IDisplayResult> UpdateAsync( | ||
InventoryPart part, | ||
IUpdateModel updater, | ||
UpdatePartEditorContext context) | ||
{ | ||
var viewModel = new InventoryPartViewModel(); | ||
if (await updater.TryUpdateModelAsync(viewModel, Prefix)) | ||
{ | ||
var currentSku = _hca.HttpContext.Request.Form["ProductPart.Sku"].ToString().ToUpperInvariant(); | ||
var skuBefore = viewModel.Inventory.FirstOrDefault().Key != null | ||
? viewModel.Inventory.FirstOrDefault().Key.Split("-").First() | ||
: "DEFAULT"; | ||
|
||
part.Inventory.Clear(); | ||
part.Inventory.AddRange(viewModel.Inventory); | ||
|
||
// If SKU was changed, inventory keys need to be updated. | ||
if (!string.IsNullOrEmpty(currentSku) && currentSku != skuBefore) | ||
{ | ||
part.InventoryKeys.Clear(); | ||
|
||
var newInventory = new Dictionary<string, int>(); | ||
var oldInventory = part.Inventory.ToDictionary(key => key.Key, value => value.Value); | ||
foreach (var inventoryEntry in oldInventory) | ||
{ | ||
var updatedKey = oldInventory.Count > 1 | ||
? currentSku + "-" + inventoryEntry.Key.Split('-').Last() | ||
: currentSku; | ||
|
||
part.Inventory.Remove(inventoryEntry.Key); | ||
newInventory.Add(updatedKey, inventoryEntry.Value); | ||
|
||
part.InventoryKeys.Add(updatedKey); | ||
} | ||
|
||
part.Inventory.Clear(); | ||
part.Inventory.AddRange(newInventory); | ||
} | ||
|
||
part.ProductSku = currentSku; | ||
} | ||
|
||
return await EditAsync(part, context); | ||
} | ||
|
||
// Despite the Clear() calls inside UpdateAsync(), the Inventory property retains its old values along with the | ||
// new ones, hence the filtering below. | ||
private static void BuildViewModel(InventoryPartViewModel model, InventoryPart part) | ||
{ | ||
var inventory = part.Inventory ?? new Dictionary<string, int>(); | ||
if (inventory.Any()) | ||
{ | ||
// Workaround for InventoryPart storing the outdated inventory entries along with the updated ones. | ||
var filteredInventory = part.Inventory.FilterOutdatedEntries(part.InventoryKeys); | ||
|
||
model.Inventory.AddRange(filteredInventory); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Modules/OrchardCore.Commerce.Inventory/Extensions/InventoryDictionaryExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace OrchardCore.Commerce.Inventory; | ||
|
||
public static class InventoryDictionaryExtensions | ||
{ | ||
public static IDictionary<string, int> FilterOutdatedEntries( | ||
this IDictionary<string, int> inventory, IList<string> inventoryKeys) => | ||
inventory | ||
.Where(inventory => inventoryKeys.Contains(inventory.Key)) | ||
.ToDictionary(key => key.Key, value => value.Value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 9 additions & 1 deletion
10
src/Modules/OrchardCore.Commerce.Inventory/Models/InventoryPart.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentManagement; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace OrchardCore.Commerce.Inventory.Models; | ||
|
||
public class InventoryPart : ContentPart | ||
{ | ||
public BooleanField AllowsBackOrder { get; set; } = new(); | ||
public BooleanField IgnoreInventory { get; set; } = new(); | ||
public NumericField Inventory { get; set; } = new(); | ||
|
||
public IDictionary<string, int> Inventory { get; } = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); | ||
|
||
public NumericField MaximumOrderQuantity { get; set; } = new(); | ||
public NumericField MinimumOrderQuantity { get; set; } = new(); | ||
public HtmlField OutOfStockMessage { get; set; } = new(); | ||
|
||
public IList<string> InventoryKeys { get; } = new List<string>(); | ||
|
||
public string ProductSku { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Modules/OrchardCore.Commerce.Inventory/ViewModels/InventoryPartViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace OrchardCore.Commerce.Inventory.ViewModels; | ||
|
||
public class InventoryPartViewModel | ||
{ | ||
public bool AllowsBackOrder { get; set; } | ||
public bool IgnoreInventory { get; set; } | ||
|
||
public IDictionary<string, int> Inventory { get; } = new Dictionary<string, int>(); | ||
|
||
public int MaximumOrderQuantity { get; set; } | ||
public int MinimumOrderQuantity { get; set; } | ||
public string OutOfStockMessage { get; set; } | ||
} |
11 changes: 0 additions & 11 deletions
11
src/Modules/OrchardCore.Commerce.Inventory/Views/InventoryPart-Inventory.cshtml
This file was deleted.
Oops, something went wrong.
8 changes: 7 additions & 1 deletion
8
src/Modules/OrchardCore.Commerce.Inventory/Views/InventoryPart-OutOfStockMessage.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Modules/OrchardCore.Commerce.Inventory/Views/InventoryPart.Edit.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@model InventoryPartViewModel | ||
|
||
<h3>@T["Inventories"]</h3> | ||
<div class="row"> | ||
@foreach (var inventoryKey in Model.Inventory.Keys) | ||
{ | ||
var inventoryTitle = Model.Inventory.Count > 1 ? inventoryKey.Split('-').Last() : string.Empty; | ||
Psichorex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<div class="col-md-3"> | ||
<div class="mb-3" asp-validation-class-for="Inventory[inventoryKey]"> | ||
<label asp-for="Inventory[inventoryKey]">@inventoryTitle @T["Inventory"] </label> | ||
<div class="input-group"> | ||
<input asp-for="Inventory[inventoryKey]" class="form-control text-muted" /> | ||
</div> | ||
<span asp-validation-for="Inventory[inventoryKey]"></span> | ||
</div> | ||
</div> | ||
} | ||
</div> |
16 changes: 16 additions & 0 deletions
16
src/Modules/OrchardCore.Commerce.Inventory/Views/InventoryPart.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@model InventoryPartViewModel | ||
|
||
@if (Model.Inventory is { } inventory) | ||
{ | ||
<div class="pb-3 field field-type-numericfield field-name-inventory-part-inventory"> | ||
@foreach (var inventoryEntry in inventory) | ||
{ | ||
var inventoryTitle = inventory.Count > 1 ? inventoryEntry.Key.Split('-').Last() : string.Empty; | ||
|
||
<span class="d-block w-100"> | ||
<strong class="field-name-inventory-part-inventory-title">@inventoryTitle @T["Inventory"]:</strong> | ||
@T["{0}", inventoryEntry.Value] | ||
</span> | ||
} | ||
</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
InventoryKeys
list property is used as a workaround to the incorrectly working dictionary property. For some reason, list properties do not retain cleared values (which is working as intended), while dictionaries do.