Create criterion for visited Commerce catalog content

Vote:
 

Hi!

I'm trying to create a visitor group criterion for when a user is visting a Commerce category, product or variant. Similar to the page visited that is built in in the CMS.

The event handler:

public override void Subscribe(ICriterionEvents criterionEvents)
{
    criterionEvents.VisitedPage += criterionEvents_VisitedPage;
}

This only triggers when I access CMS content and not Commerce content? I guess there might be a ICriterionEvents for Commerce content or should the one in EPiServer.Personalization.VisitorGroups also handle Commerce content?

Thanks!

/Kristoffer

#278125
Edited, Apr 08, 2022 13:28
Vote:
 

How about simply decorating your category, product and variant controllers with [ViewedPageCriterion]?

#278330
Apr 11, 2022 19:58
Vote:
 

Ok, I will try first thing tomorrow!

#278331
Apr 11, 2022 20:02
Vote:
 

Oh I just commented the same thing on your ticket :). While I'm not 100% sure, it seems to be the way 

#278372
Apr 12, 2022 9:11
Vote:
 

Well that actually triggerd: 

private criterionEvents_VisitedPage(object sender, CriterionEventArgs e)

But since e.GetPageLink(); gets you a PageReference it wont work. It always gives me the start page when visiting Commerce content.

Maybe I can use e.HttpContext in some way to get the actually visited Commerce content or how do I do to get the content?

I tried to rip and rewrite some code from e.GetPageLink:

var contentReference = e.HttpContext.GetService<IContentRouteHelper>().ContentLink

But it wont compile with the error: The non-generic method 'HttpContextBase.GetService(Type)' cannot be used with type arguments

Thanks!

/Kristoffer

#278375
Apr 12, 2022 15:59
Vote:
 

Sure, you can use HttpContext.RequestContext.GetRoutedData<IContent>() if you want the actual content. Then you can simply check if that content is a Catalog, Product or Variant by doing smth like:

var content = e.HttpContext.RequestContext.GetRoutedData<IContent>();
if (content is KristoffersProductContent product)
{
  // implement
}

If you only want the content link, you can simply run e.HttpContext.RequestContext.GetContentLink().

#278376
Edited, Apr 12, 2022 17:12
Vote:
 

Thanks for the guidance, this is what solved it:

var c = e.HttpContext.Request.RequestContext.GetContentLink();

In combination with 

[ViewedPageCriterion]
public class CategoryController : ContentController<Node> { }

Thanks all for your help!

/Kristoffer

#278424
Apr 13, 2022 8:36
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.