Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New plugin: Vendure-plugin-primary-collection #257

Closed
martijnvdbrug opened this issue Sep 14, 2023 · 0 comments · Fixed by #258
Closed

New plugin: Vendure-plugin-primary-collection #257

martijnvdbrug opened this issue Sep 14, 2023 · 0 comments · Fixed by #258
Assignees
Labels
feature New feature

Comments

@martijnvdbrug
Copy link
Member

Is your feature request related to a problem? Please describe.

To construct breadcrumbs and URL's it's useful to have a primary collection for each product, in case a product is part of multiple collections.

Describe the solution you'd like

  • I would like the field Product.primaryCollection to return the primary collection of a product
  • The primary collection of a product is the collection that is the highest placed collection in Vendure (Collection's are sortable in Vendure, and it's a good practice to sort by importance).
  • E2e test: Create 2 categories, place product in both categories, querying product.primaryCollection should always be the highest placed collection.
  • Code has already been used in another project, ready to copy/paste. See below for snippet.

Additional context

Shop API schema:

  extend type Product {
    primaryCollection: Collection
  }

Resolver code (feel free to change or improve where necessary):

import { Parent, ResolveField, Resolver } from "@nestjs/graphql";
import {
  Collection,
  CollectionService,
  Ctx,
  RequestContext,
  Product,
} from "@vendure/core";

@Resolver("Product")
export class PrimaryCollectionResolver {
  constructor(private readonly collectionService: CollectionService) {}

  @ResolveField()
  async primaryCollection(
    @Ctx() ctx: RequestContext,
    @Parent() product: Product,
  ): Promise<Collection | null> {
    const collections = await this.collectionService.getCollectionsByProductId(
      ctx,
      product.id,
      true,
    );
    const collectionsExcludingParents = collections.filter(
      (coll) =>
        !collections.find((childColl) => childColl.parentId === coll.id),
    );
    return collectionsExcludingParents.reduce<Collection | null>(
      (acc, val) => {
        if (!acc) {
          return val;
        }
        if (val.position < acc.position) {
          return val;
        }
        return acc;
      },
      null
    );
  }
}

image
⬆️ Sort by most popular according to Googles UX retail playbook

@martijnvdbrug martijnvdbrug added the feature New feature label Sep 14, 2023
@dalyathan dalyathan linked a pull request Sep 15, 2023 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants