Related Products by Category

Vote:
 

I would like to get some related products, when publishing a product page. I've specified some product categories, so two product are related products if they have at least one common category.

I've tried the following:

- get product pages with same categories as the actual page to be published. I've the following code:

void PublishedContentHandler(object sender, ContentEventArgs e)
      {
 
          if (e.Content.GetType().Name == "ProductPageProxy")
          {
              var productpage = (ProductPage)e.Content;
              var selectedItem = productpage.Category;
              var cat = new CategoryHelper();
              var pages = DataFactory.Instance.FindPagesWithCriteria(PageReference.StartPage, cat.SelectedCriteria(selectedItem.ToString()));
              productpage.RelatedContentArea = new ContentArea();
 
              foreach (var page in pages)
              {
                  productpage.RelatedContentArea.Add(page);
              }
 
// at that moment I have in variable pages the productpages to add to relatedcontentarea, and it's ok, but from now on it's not working...no pages saved on relatedcontentarea of productpage I'm publishing
              e.Content = productpage;               e.Content.GetForceCurrentVersionSaveAction();           }       }

#141270
Nov 10, 2015 18:38
Vote:
 

Hi,

If you use SavingContent event then you could modify the content properties before they will be saved.

But you are using PublishedContent event - setting properties here without explicit saving content has no effect.

The GetForceCurrentVersionSaveAction() extension method is not responsible for saving content. It's just returns the proper SaveAction for content instance. 

I think that you could try to use:

 ServiceLocator.Current.GetInstance<IContentRepository>()
                .Save(productPage, productPage.GetForceCurrentVersionSaveAction());
#141285
Nov 11, 2015 11:27
Vote:
 

Thank you!

#141313
Nov 11, 2015 19:24
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.