Include unpublished products in the index

Created on 16 August 2023, about 1 year ago
Updated 14 November 2023, about 1 year ago

Problem/Motivation

In the site I'm building, products will be unpublished most of the time. Having installed this module and tried to filter a view of products by taxonomy term with depth, I found the view returned no results.

Steps to reproduce

  • Install the module on a site with a number of unpublished products.
  • Create a view that returns unpublished products.
  • Add an exposed filter "Product: Has taxonomy term ID (with depth)".
  • The view will not return any of the unpublished products as I would expect it would.

Proposed resolution

When commerce_product_taxonomy_filter_install() is called, an entity query is called:

$query = \Drupal::entityQuery('commerce_product')
    ->accessCheck(TRUE)
    ->condition('status', 1);

The condition 'status', 1 means only published products will be returned. These products are then fed to commerce_product_taxonomy_filter_build_commerce_product_index(). Any unpublished products won't have their taxonomy terms indexed. Further to this, there is another check within this function (commerce_product_taxonomy_filter.module, lines 69-73):

  $status = $commerce_product->isPublished();
  // We only maintain the taxonomy index for published commerce_products.
  if ($status && $commerce_product->isDefaultRevision()) {
    // Collect a unique list of all the tids from all commerce_product fields.
    $tid_all = [];

And even a helpful comment explaining we only maintain the taxonomy index for published products (which I've only spotted just now -_-)
What's the reasoning behind this, and would you consider committing a patch that changes this?
The core equivalent filter in the core Taxonomy module doesn't behave this way, and we have separate filters for published / unpublished status.

โœจ Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom griz

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024