Deleting product type
To delete a product type, you must perform the following:
- Get an instance of the following managers:
- Catalog manager
- Catalog definition manager
- Get the specified product type.
Get an instance of the product type to be deleted. For more information, read Querying product types.
- Check if there are products associated with the product type.
If there are products associated with the product type, you must first delete them. For more information, see Products.
- Remove the UI definition for the product type.
To remove the product type from the catalog definition, call the RemoveProductTypeDefinition method of the catalog definition manager and pass the product type as an argument.
- Mark the product type for deletion.
To mark the product type to be deleted, call the DeleteProduct method of the catalog manager and pass the ID of the product as an argument.
- Save the changes.
To delete the product type, save the changes to the catalog manager.
- Adjust the catalog definition for a single product type.
If after deleting the product type, there is only one type left, you must adjust the catalog definition for a single product type. To do this, call the AdjustForSingleProductType method of the catalog definition manager and pass the remaining product type as an argument.
TIP: To delete multiple product types, query them and execute the code below for each one of them.
NOTE: If the product type has products associated with it, you must first delete the products.
Here is a code example:
public static void DeleteProductType(Guid productTypeId)
{
CatalogManager catalogManager = CatalogManager.GetManager();
CatalogDefinitionManager catalogDefinitionManager = new CatalogDefinitionManager();
ProductType productType = catalogManager.GetProductType(productTypeId);
if (catalogManager.GetProducts(productType.ClrType).Any())
{
return;
}
catalogDefinitionManager.RemoveProductTypeDefinition(productType);
catalogManager.DeleteProductType(productTypeId);
catalogManager.SaveChanges();
if (catalogManager.GetProductTypes().Count() == 1)
{
catalogDefinitionManager.AdjustForSingleProductType(catalogManager.GetProductTypes().Single());
}
}