magento2 – How can I do product sorting on configurable’s child product attribute data depending on the layered navigation filter selection

I have a particular requirement where I need to do product sorting on configurable’s child product attribute data depending on the layered navigation filter selection.

The following illustration will help to understand the requirement in a better way.

  • Have a site that offers some courses, it is represented by a configurable product in M2.
  • Each course runs multiple times in a year, so they are child virtual products assigned to the configurable product.
  • Each course occurrence (child product) has a start date.
  • The “soonest” start date is held in an attribute against the parent course (latest date from children) and when we’re not filtering we can sort by this date and results are fine. For example, if we searched today you’d see courses starting next week at the top of the list.
  • When we add in a filter for a month like “September” the results returned will show the courses that have an occurrence happening in September – so this is fine. But the search by soonest is still looking at the earliest course start date.
    For example, course A had an occurrence on June 5th and one in September on September 20th, and course B had June 20th and September 5th.
    So the customer expects course B to show first because it starts sooner in September but the sort is still looking at that overall soonest date – parent not child.

So the problem is – how do we use the occurrence date of the child product when we’ve filtered. When I tried to set the child value into the layer collection item through customer code, it breaks the pagination.

Any help would be appreciated.