Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Listening changes in parents to update custom variant fields.

Vote:
 

Hello,

I have created extension methods on my variant content like ProductDescription, ProductStyle, and ProductSubStyle. The values come from the parent product. If I update a variant item, their custom properties are indexed correctly in Epifind, but if I update their products (e.g. ProductDescription field), the custom field in the variants is not updated. How can I include the variant dependencies of products when indexing?

#223752
Jun 03, 2020 16:06
Vote:
 

You can add an indexing command in the OnPublished event for all variants of the products? Something like this

private static void AddToIndex(IEnumerable<IContent> itemsToAdd)
{
  IContentIndexer contentIndexer = ServiceLocator.Current.GetInstance<IContentIndexer>();

  contentIndexer.Index(
			contentItems: itemsToAdd,
			options: new IndexOptions { IndexAllLanguageVersions = true });
}
#223780
Jun 04, 2020 7:29
Vote:
 

You can listen to IContentEvents.PublishedContent, check if the content published is Product or not, if yes, use IRelationRepository to load its variants, then index those.

#223801
Jun 04, 2020 15:26
Vote:
 

Thank you so much Jeroen and Quan that work marvelously. I am posting the complete code for future references:

[InitializableModule]
[ModuleDependency(typeof(ServiceContainerInitialization))]
public class DependencyResolution : IConfigurableModule
{
    private IContentEvents _contentEvents;
    private Injected<IRelationRepository> _relationRepository;
    private Injected<IContentLoader> _contentLoader;
    private Injected<IContentIndexer> _contentIndexer;

    public void Initialize(InitializationEngine context)
    {
        if (_contentEvents == null)
        {
            _contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
        }
        
        _contentEvents.PublishedContent += contentEvents_PublishedContent;
    }

    void contentEvents_PublishedContent(object sender, ContentEventArgs e)
    {
        if (e.Content is ProductContent product)
        {
            var variants = _relationRepository.Service.GetChildren<ProductVariation>(product.ContentLink);
            AddToIndex(variants.Select(x => _contentLoader.Service.Get<IContent>(x.Child)));
        }
    }

    private void AddToIndex(IEnumerable<IContent> itemsToAdd)
    {
        _contentIndexer.Service.Index(
            contentItems: itemsToAdd,
            options: new IndexOptions { IndexAllLanguageVersions = true });
    }

    public void Uninitialize(InitializationEngine context)
    {
        if (_contentEvents == null)
        {
            _contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
        }

        _contentEvents.PublishedContent -= contentEvents_PublishedContent;
    }

    public void ConfigureContainer(ServiceConfigurationContext context)
    {
    }
}
#223805
Jun 04, 2020 20:19
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.