Skip to content

Commit a30ab5d

Browse files
committed
Allow a store owner to specify display order to associated products (for "grouped" products)
1 parent 934d819 commit a30ab5d

11 files changed

Lines changed: 74 additions & 13 deletions

File tree

src/Libraries/SmartStore.Core/Domain/Catalog/Product.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,12 @@ public Product()
516516
[DataMember]
517517
public DateTime? AvailableEndDateTimeUtc { get; set; }
518518

519+
/// <summary>
520+
/// Gets or sets a display order. This value is used when sorting associated products (used with "grouped" products)
521+
/// </summary>
522+
[DataMember]
523+
public int DisplayOrder { get; set; }
524+
519525
/// <summary>
520526
/// Gets or sets a value indicating whether the entity is published
521527
/// </summary>

src/Libraries/SmartStore.Services/Catalog/CopyProductService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ public virtual Product CopyProduct(Product product, string newName, bool isPubli
197197
Height = product.Height,
198198
AvailableStartDateTimeUtc = product.AvailableStartDateTimeUtc,
199199
AvailableEndDateTimeUtc = product.AvailableEndDateTimeUtc,
200+
DisplayOrder = product.DisplayOrder,
200201
Published = isPublished,
201202
Deleted = product.Deleted,
202203
CreatedOnUtc = DateTime.UtcNow,

src/Libraries/SmartStore.Services/Catalog/ProductService.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -605,18 +605,14 @@ orderby pGroup.Key
605605
//manufacturer position
606606
query = query.OrderBy(p => p.ProductManufacturers.Where(pm => pm.ManufacturerId == ctx.ManufacturerId).FirstOrDefault().DisplayOrder);
607607
}
608-
//else if (orderBy == ProductSortingEnum.Position && relatedToProductId > 0)
609-
//{
610-
// //sort by related product display order
611-
// query = from p in query
612-
// join rp in _relatedProductRepository.Table on p.Id equals rp.ProductId2
613-
// where (relatedToProductId == rp.ProductId1)
614-
// orderby rp.DisplayOrder
615-
// select p;
616-
//}
608+
else if (ctx.OrderBy == ProductSortingEnum.Position && ctx.ParentProductId > 0)
609+
{
610+
//parent product specified (sort associated products)
611+
query = query.OrderBy(p => p.DisplayOrder);
612+
}
617613
else if (ctx.OrderBy == ProductSortingEnum.Position)
618614
{
619-
//sort by name (there's no any position if category or manufactur is not specified)
615+
//otherwise sort by name
620616
query = query.OrderBy(p => p.Name);
621617
}
622618
else if (ctx.OrderBy == ProductSortingEnum.NameAsc)

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,8 @@ public ActionResult AssociatedProductList(GridCommand command, int productId)
14781478
return new ProductModel.AssociatedProductModel()
14791479
{
14801480
Id = x.Id,
1481-
ProductName = x.Name
1481+
ProductName = x.Name,
1482+
DisplayOrder = x.DisplayOrder
14821483
};
14831484
})
14841485
.ToList();
@@ -1495,6 +1496,22 @@ public ActionResult AssociatedProductList(GridCommand command, int productId)
14951496
};
14961497
}
14971498

1499+
[GridAction(EnableCustomBinding = true)]
1500+
public ActionResult AssociatedProductUpdate(GridCommand command, ProductModel.AssociatedProductModel model)
1501+
{
1502+
if (!_permissionService.Authorize(StandardPermissionProvider.ManageCatalog))
1503+
return AccessDeniedView();
1504+
1505+
var associatedProduct = _productService.GetProductById(model.Id);
1506+
if (associatedProduct == null)
1507+
throw new ArgumentException("No associated product found with the specified id");
1508+
1509+
associatedProduct.DisplayOrder = model.DisplayOrder;
1510+
_productService.UpdateProduct(associatedProduct);
1511+
1512+
return AssociatedProductList(command, associatedProduct.ParentProductId);
1513+
}
1514+
14981515
[GridAction(EnableCustomBinding = true)]
14991516
public ActionResult AssociatedProductDelete(int id, GridCommand command)
15001517
{

src/Presentation/SmartStore.Web/Administration/Models/Catalog/ProductModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ public partial class AssociatedProductModel : EntityModelBase
474474
{
475475
[SmartResourceDisplayName("Admin.Catalog.Products.AssociatedProducts.Fields.Product")]
476476
public string ProductName { get; set; }
477+
478+
[SmartResourceDisplayName("Admin.Catalog.Products.AssociatedProducts.Fields.DisplayOrder")]
479+
public int DisplayOrder { get; set; }
477480
}
478481
public partial class AddAssociatedProductModel : ModelBase
479482
{

src/Presentation/SmartStore.Web/Administration/Views/Product/_CreateOrUpdate.AssociatedProducts.cshtml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
{
2828
dataBinding.Ajax()
2929
.Select("AssociatedProductList", "Product", new { productId = Model.Id })
30+
.Update("AssociatedProductUpdate", "Product")
3031
.Delete("AssociatedProductDelete", "Product");
3132
})
3233
.Columns(columns =>
@@ -35,11 +36,14 @@
3536
.ReadOnly();
3637
columns.Bound(x => x.ProductName)
3738
.ReadOnly();
39+
columns.Bound(x => x.DisplayOrder)
40+
.Centered();
3841
columns.Command(commands =>
3942
{
43+
commands.Edit().Localize(T);
4044
commands.Delete().Localize(T);
4145
})
42-
.Width(90);
46+
.Width(180);
4347
})
4448
.EnableCustomBinding(true))
4549
</td>

src/Presentation/SmartStore.Web/App_Data/Migrations/1.2.1-1.3.0/3-migrate-improved-product-structure-resources.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,12 @@ SET @resources='
18901890
<Value>Search by a product type.</Value>
18911891
<Value lang="de">Nach einem Produkttyp suchen.</Value>
18921892
</LocaleResource>
1893+
1894+
</LocaleResource>
1895+
<LocaleResource Name="Admin.Catalog.Products.AssociatedProducts.Fields.DisplayOrder">
1896+
<Value>Display order</Value>
1897+
<Value lang="de">Reihenfolge</Value>
1898+
</LocaleResource>
18931899
18941900
</Language>
18951901
'

src/Presentation/SmartStore.Web/App_Data/Migrations/1.2.1-1.3.0/4-migrate-improved-product-structure-core.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,19 @@ BEGIN
21442144
END
21452145
GO
21462146

2147+
--new [DisplayOrder] property
2148+
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id=object_id('[Product]') and NAME='DisplayOrder')
2149+
BEGIN
2150+
ALTER TABLE [Product]
2151+
ADD [DisplayOrder] int NULL
2152+
END
2153+
GO
2154+
2155+
UPDATE [Product] SET [DisplayOrder] = 0
2156+
GO
2157+
ALTER TABLE [Product] ALTER COLUMN [DisplayOrder] int NOT NULL
2158+
GO
2159+
21472160
--updated product type values
21482161
UPDATE [Product] SET [ProductTypeId]=5 WHERE [ProductTypeId]=0
21492162
GO
@@ -2668,6 +2681,13 @@ BEGIN
26682681
SET @sql_orderby = @sql_orderby + ' pmm.DisplayOrder ASC'
26692682
END
26702683

2684+
--parent product specified (sort associated products)
2685+
IF @ParentProductId > 0
2686+
BEGIN
2687+
IF LEN(@sql_orderby) > 0 SET @sql_orderby = @sql_orderby + ', '
2688+
SET @sql_orderby = @sql_orderby + ' p.[DisplayOrder] ASC'
2689+
END
2690+
26712691
--name
26722692
IF LEN(@sql_orderby) > 0 SET @sql_orderby = @sql_orderby + ', '
26732693
SET @sql_orderby = @sql_orderby + ' p.[Name] ASC'

src/Presentation/SmartStore.Web/App_Data/SqlServer.StoredProcedures.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,13 @@ BEGIN
575575
SET @sql_orderby = @sql_orderby + ' pmm.DisplayOrder ASC'
576576
END
577577

578+
--parent product specified (sort associated products)
579+
IF @ParentProductId > 0
580+
BEGIN
581+
IF LEN(@sql_orderby) > 0 SET @sql_orderby = @sql_orderby + ', '
582+
SET @sql_orderby = @sql_orderby + ' p.[DisplayOrder] ASC'
583+
END
584+
578585
--name
579586
IF LEN(@sql_orderby) > 0 SET @sql_orderby = @sql_orderby + ', '
580587
SET @sql_orderby = @sql_orderby + ' p.[Name] ASC'

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,6 @@ protected ProductDetailsModel PrepareProductDetailsPageModel(Product product, bo
10411041
var searchContext = new ProductSearchContext()
10421042
{
10431043
StoreId = _storeContext.CurrentStore.Id,
1044-
OrderBy = ProductSortingEnum.NameAsc,
10451044
ParentProductId = product.Id,
10461045
VisibleIndividuallyOnly = false
10471046
};

0 commit comments

Comments
 (0)