---
agentTools:
  projectIndex: https://merchello.readme.io/llms.txt
---

# ProductService

Documentation for Merch's ProductService

The ProductService is used to manage products in the back office product catalog.

[block:api-header]
{
  "type": "basic",
  "title": "Instantiating",
  "sidebar": true
}
[/block]

[block:textarea]
{
  "text": "The `ProductService` is exposed through the `MerchelloContext`",
  "sidebar": true
}
[/block]

[block:code]
{
  "codes": [
    {
      "code": "var productService = MerchelloContext.Current.Services.ProductService;",
      "language": "csharp"
    }
  ],
  "sidebar": true
}
[/block]

[block:api-header]
{
  "type": "basic",
  "title": "Class"
}
[/block]

[block:code]
{
  "codes": [
    {
      "code": "public class ProductService : PageCachedServiceBase<IProduct>, \n\tIProductService, IStaticCollectionService<IProduct>, IPageCachedService<IProduct>, \n\tIService",
      "language": "csharp"
    }
  ]
}
[/block]

[block:api-header]
{
  "type": "basic",
  "title": "Constructors"
}
[/block]

[block:parameters]
{
  "data": {
    "h-0": "Name",
    "h-1": "Description",
    "0-0": "ProductService()",
    "1-0": "ProductService(ILogger)",
    "2-0": "ProductService(RepositoryFactory, ILogger, IProductVariantService)",
    "3-0": "ProductService(IDatabaseUnitOfWorkProvider, RepositoryFactory, ILogger, IProductVariantService)",
    "4-0": "ProductService(IDatabaseUnitOfWorkProvider, RepositoryFactory, ILogger, IEventMessagesFactory, IProductVariantService)",
    "0-1": "Initializes a new instance of the ProductService class",
    "1-1": "Initializes a new instance of the ProductService class",
    "2-1": "Initializes a new instance of the ProductService class",
    "3-1": "Initializes a new instance of the ProductService class",
    "4-1": "Initializes a new instance of the ProductService class"
  },
  "cols": 2,
  "rows": 5
}
[/block]

[block:api-header]
{
  "type": "basic",
  "title": "Public Methods"
}
[/block]

[block:parameters]
{
  "data": {
    "h-0": "Name",
    "h-1": "Description",
    "0-0": "AddToCollection(Guid, Guid)",
    "1-0": "AddToCollection(IProduct, IEntityCollection)",
    "2-0": "AddToCollection(IProduct, Guid)",
    "3-0": "CreateProduct",
    "4-0": "CreateProductWithKey",
    "5-0": "Delete(IProduct, Boolean)",
    "6-0": "Delete(IEnumerable(IProduct), Boolean)",
    "7-0": "ExistsInCollection",
    "8-0": "GetAll",
    "9-0": "GetByDetachedContentType",
    "10-0": "GetByKey",
    "11-0": "GetByKeys",
    "12-0": "GetBySku",
    "13-0": "GetFromCollection(Guid, Int64, Int64, String, SortDirection)",
    "14-0": "GetFromCollection(Guid, String, Int64, Int64, String, SortDirection)",
    "15-0": "GetPage",
    "16-0": "GetProductVariantByKey",
    "17-0": "GetProductVariantBySku",
    "18-0": "GetProductVariantsByProductKey",
    "19-0": "ProductsCount",
    "20-0": "RemoveDetachedContent(IProduct, Guid, Boolean)",
    "21-0": "RemoveDetachedContent(IEnumerable(IProduct), Guid, Boolean)",
    "22-0": "RemoveFromCollection(Guid, Guid)",
    "23-0": "RemoveFromCollection(Guid, Guid)",
    "24-0": "RemoveFromCollection(IProduct, Guid)",
    "25-0": "Save(IProduct, Boolean)",
    "26-0": "Save(IEnumerable(IProduct), Boolean)",
    "26-1": "Saves a collection of IProduct",
    "25-1": "Saves a collection of IProduct",
    "24-1": "Remove product from a collection",
    "23-1": "Remove product from a collection",
    "22-1": "Remove product from a collection",
    "21-1": "Removes detached content from the collection of products",
    "20-1": "Removes detached content from the collection of products",
    "19-1": "**Obsolete. ** Returns the count of all products",
    "18-1": "Gets  a collection of IProductVariant by product.",
    "17-1": "Get's a IProductVariant by it's unique SKU.",
    "16-1": "Gets a IProductVariant by it's key.",
    "15-1": "Gets a page of IProduct",
    "14-1": "Gets products from a collection filtered by a search term.",
    "13-1": "Gets products from a collection.",
    "12-1": "Gets an IProduct by it's unique SKU.",
    "11-1": "Gets a list of Product give a list of unique keys",
    "10-1": "Gets a Product by its unique id - primary key",
    "9-1": "Gets a collect of products by detached content type",
    "8-1": "Gets all the products",
    "7-1": "Returns a value indicating whether or not a product exists in a colleciton",
    "6-1": "Deletes a collection IProduct",
    "5-1": "Deletes a single IProduct",
    "4-1": "Creates and saves a IProduct to the database",
    "3-1": "Creates a Product without saving it to the database",
    "2-1": "Adds a product to a collection",
    "0-1": "Adds a product to a collection",
    "1-1": "Adds a product to a collection"
  },
  "cols": 2,
  "rows": 27
}
[/block]

[block:api-header]
{
  "type": "basic",
  "title": "Events"
}
[/block]

[block:parameters]
{
  "data": {
    "h-0": "Name",
    "h-1": "Description",
    "0-0": "Creating",
    "0-1": "Occurs before create",
    "1-0": "Created",
    "1-1": "Occurs after create",
    "2-0": "Deleting",
    "2-1": "Occurs before delete",
    "3-0": "Deleted",
    "3-1": "Occurs after delete",
    "4-0": "Saving",
    "4-1": "Occurs before save",
    "5-0": "Saved",
    "5-1": "Occurs after save"
  },
  "cols": 2,
  "rows": 6
}
[/block]

Example of using the Saved event (Events must be added to the ApplicationStarting event)

[block:code]
{
  "codes": [
    {
      "code": "ProductService.Saved += ProductServiceSaved;",
      "language": "csharp"
    }
  ]
}
[/block]

Then the method

[block:code]
{
  "codes": [
    {
      "code": "        private static void ProductServiceSaved(IProductService sender, SaveEventArgs<IProduct> e)\n        {\n            foreach (var p in e.SavedEntities)\n            {\n                // do what you want\n            }\n        }",
      "language": "csharp"
    }
  ]
}
[/block]

[block:api-header]
{
  "type": "basic",
  "title": "Interface on GitHub"
}
[/block]

[block:embed]
{
  "html": false,
  "url": "https://github.com/Merchello/Merchello/blob/master/src/Merchello.Core/Services/Interfaces/IProductService.cs",
  "title": "Merchello/Merchello",
  "favicon": "https://assets-cdn.github.com/favicon.ico",
  "image": "https://avatars2.githubusercontent.com/u/6453072?v=3&s=400"
}
[/block]

<https://github.com/Merchello/Merchello/blob/master/src/Merchello.Core/Services/Interfaces/IProductService.cs>