commercetools

How can we add one more ProductType to Product?


I recently work with Commercetools and I have a question.

How can we add one more ProductType to Product? I've heard it can be implemented by attribute reference. So, I'm trying to do this way:

final Reference<ProductType> productTypeReference = ProductType.referenceOfId(request.getProductTypeId()).toReference();
        final AttributeDraft draft = AttributeDraft.of(request.getAttributeName(), productTypeReference);
        final SetAttributeInAllVariants attribute = SetAttributeInAllVariants.of(draft);
        return ProductUpdateCommand.of(product, attribute);

But it does not work? How it can be implemented?


Solution

  • While referencing a productType is possible, it may be more advisable to create a single productType that contains all of the attributes you wish to have.

    You can reference a prouductType which is outlined here: https://docs.commercetools.com/http-api-types#references

    However, to return the data from the reference you'll need to expand the reference which is outlined here: https://docs.commercetools.com/http-api#reference-expansion

    Does this help?

    Best Regards, Michael