vendure-ecommerce / vendure

The commerce platform with customization in its DNA.
https://www.vendure.io
Other
5.64k stars 1k forks source link

Add toggle to turn on inheritance of collection from child to parent #1743

Open Izayda opened 2 years ago

Izayda commented 2 years ago

Is your feature request related to a problem? Please describe. I want to be able to make collections, that inherit filters filters from children.

Example: — Chairs and sofas (take all filters of direct children) — — Chairs (filter for chairs) — — Sofas (filter for sofas)

Describe the solution you'd like Add this option for collection (as on of the variant of new option - On the v2.0 branch there is a new toggle that allows you to turn off this filter inheritance behaviour).

Describe alternatives you've considered Make a own filters for all collections.

michaelbromley commented 1 year ago

Note: a work-around for this is to create a collection filter which allows you to populate based on the contents of other collections. So then you can choose the child collections to simulate the behaviour described here:

import { CollectionFilter, LanguageCode, ProductVariant } from '@vendure/core';

export const collectionContentsFilter = new CollectionFilter({
    args: {
        collectionIds: {
            label: [{ languageCode: LanguageCode.en, value: 'Collections' }],
            type: 'ID',
            list: true,
            // this UI form input is left as an exercise for the reader
            ui: { component: 'collection-form-input' },
        },
    },
    code: 'child-contents-filter',
    description: [{ languageCode: LanguageCode.en, value: 'Populate with contents of selected collections' }],
    apply: (qb, args) => {
        if (args.collectionIds.length === 0) {
            return qb;
        }
        const subquery = qb.connection
            .createQueryBuilder(ProductVariant, 'productVariant2')
            .leftJoin('productVariant2.collections', 'collection2')
            .where('collection2.id IN (:...collectionIds)')
            .select('productVariant2.id')
            .getQuery();
        return qb
            .andWhere(`productVariant.id IN (${subquery})`)
            .setParameters({ collectionIds: args.collectionIds });
    },
});
JamesLAllen commented 5 months ago

I'm not sure how different this workaround is from just trying to list all of the facets found in the children which can be tedious. The most common use-case I've seen for nested Collections are Categories, using a single category:Category Name as the facet for the collection. If you want all of the products to list in the parent's listing page, you have to list all of the facets that it contains, as well as all of the grandchildren and descendants. I created a way to find all of the sub-children and apply them but it's also prone to risk trying to apply them all. It would be nice to just toggle "inherit children" just like we can with "inherit parent".

image