1

What is the approach if want to add a new attribute to the Product(Model/Data/DTO) in SAP Commerce Cloud and wanna to access it in Spartacus (using Spartacus ProductService)? How to introduce the attribute to the Product model in Spartacus and get it populated with the value from the backend?

This question can be seen as a general question how to apply this requirement to all models and keeping the models in sync between backend and frontend.

Thank you in advance.

1 Answers1

0

In order to fetch additional attributes, you can configure the endpoint. See https://sap.github.io/cloud-commerce-spartacus-storefront-docs/connecting-to-other-systems/#configuring-endpoints for more information. There's no need to convert (normalize) the data necessarily, but you could do this as well. This is covered in the same documentation. And you could even replace the standard OCC adapters by a custom adapter if you need to adapt a 3rd party backend.

Once the data is loaded from the backend, it will be stored in the central store, and exposed by the facade without limitation. You might however want to enhance the default typing, in order to benefit from type-safety and not fallback to any. You can do this with the following:

// intro custom typing
interface CustomProduct extends Product {
  customAttribute?: string;
}

// use typing for the observed data
product$: Observable<CustomProduct> = this.currentProductService.getProduct()
tobi-or-not-tobi
  • 1,250
  • 1
  • 8
  • 10
  • @tobi-or-not-tobi this does not work in Typescript strict mode as the service returns a type Product | null and not the type CustomProduct.. – user2477219 Dec 30 '20 at 09:18
  • @user2477219 That's correct, we've introduced type augmentation to that reason, so that you can augment the default product type instead of bringing your own. See https://sap.github.io/spartacus-docs/type-augmentation/ for more info on that. – tobi-or-not-tobi Dec 31 '20 at 10:57
  • thanks, thought so..just wanted to make sure, type augmentation is the only way it works.. – user2477219 Jan 01 '21 at 13:12