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

ProductService.Saving event contains already saved products #2113

Open
ronaldbarendse opened this Issue Dec 14, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@ronaldbarendse

ronaldbarendse commented Dec 14, 2017

I'm creating media folders based on the product SKU, so editors won't have to select images for every language (and just put the product images/attachments in the matching folder).

The folders are successfully created and I've added code to handle renaming the SKU, but I've noticed that the products in SavedEntities within the Saving event are already saved: .IsDirty is always false and the previous saved product (retrieved using productService.GetByKey()) has the same values...

I've tested the following code in a new project using Umbraco 7.7.7 and Merchello 2.6.0:

using System.Linq;
using Merchello.Core.Models;
using Merchello.Core.Services;
using Umbraco.Core;
using Umbraco.Core.Events;

public class CustomApplicationEventHandler : ApplicationEventHandler
{
	protected const string ProductsMediaFolder = "Products";

	protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
	{
		base.ApplicationStarting(umbracoApplication, applicationContext);

		ProductService.Saving += ProductService_Saving;
	}

	protected void ProductService_Saving(IProductService productService, SaveEventArgs<IProduct> e)
	{
		var mediaService = ApplicationContext.Current.Services.MediaService;

		// Ensure parent folder exists
		var parentFolder = mediaService.GetRootMedia().FirstOrDefault(m => m.ContentType.Alias == Constants.Conventions.MediaTypes.Folder && m.Name == ProductsMediaFolder);
		if (parentFolder == null)
		{
			parentFolder = mediaService.CreateMedia(ProductsMediaFolder, -1, Constants.Conventions.MediaTypes.Folder);
			mediaService.Save(parentFolder);
		}

		// Get existing folders
		var existingMediaFolders = mediaService.GetChildren(parentFolder.Id).Where(m => m.ContentType.Alias == Constants.Conventions.MediaTypes.Folder).ToList();

		// Create folder for products
		foreach (var product in e.SavedEntities)
		{
			var productFolder = existingMediaFolders.FirstOrDefault(m => m.Name == product.Sku);
			if (productFolder == null && product.HasIdentity)
			{
				// Check if we need to rename existing folder
				var previousProduct = productService.GetByKey(product.Key);
				if (previousProduct != null && previousProduct.Sku != product.Sku) // This is never true, because the product is already saved!
				{
					productFolder = existingMediaFolders.FirstOrDefault(m => m.Name == previousProduct.Sku);
					if (productFolder != null)
					{
						productFolder.Name = product.Sku;
					}
				}
			}

			if (productFolder == null)
			{
				// Create new folder
				productFolder = mediaService.CreateMediaWithIdentity(product.Sku, parentFolder, Constants.Conventions.MediaTypes.Folder);
			}

			mediaService.Save(productFolder);
		}
	}
}

If it's possible to make the Extended Content not translatable, that would be even better! Than I could use a media picker together with Vorto for the translatable content. Switching languages in Merchello is not very user friendly currently, for example: why is it tucked away in the Actions button and only showing the active language in the footer/breadcrumb bar? If there's already plans for updating the interface (moving product info, extended content and product options to navigation tabs), please let me know!

@ronaldbarendse ronaldbarendse changed the title from ProductService.Saving event already contains saved products to ProductService.Saving event contains already saved products Dec 14, 2017

@ronaldbarendse

This comment has been minimized.

ronaldbarendse commented Jan 30, 2018

Is there no one that has the same problem or that could at least verify this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment