Extending product model in Spartacus

天大地大妈咪最大 提交于 2021-01-29 08:20:59

问题


I'm trying to extend the product model with a new attribute I created in the -items.xml, but I can't seem to get it in Spartacus front. I added a product.model.ts file with the following code:

import { Product as CxProduct } from '@spartacus/core';

export interface Product extends CxProduct {
  myAttribute: string;
}

And I imported that file in the pipe where I use the product model, but it appears empty althoughs the product does have data in myAttribute field in backoffice. Is there something I may be missing?

I'm using this attribute in a product-images.component I created to override the default one. This is the new product-images.component.ts

import { Component, OnInit } from '@angular/core';
import { CurrentProductService } from '@spartacus/storefront';
import { ProductImagesComponent as SpartacusProductImages } from '@spartacus/storefront'

@Component({
  selector: 'cx-product-images',
  templateUrl: './product-images.component.html',
  styleUrls: ['./product-images.component.scss']
})
export class ProductImagesComponent extends SpartacusProductImages implements OnInit {

  constructor(currentProductService: CurrentProductService) {
    super(currentProductService)
  }

  ngOnInit() {
  }


}

Thanks a lot


回答1:


You need ask backend explicitly to return you the attribute. Please configure the product endpoint to contain ?fields= with your custom attribute. See docs: https://sap.github.io/spartacus-docs/connecting-to-other-systems/#configuring-endpoints

Please mind that you can specify various scoped endpoint configuraitons for product data - so you can load the custom attribute only when needed (i.e. it's useful when myAttribute is not displayed for some views like product carousel or product list). But since you're manipulating the image data, I guess you want to use this property everywhere (so please use scope called list).

For more, see docs for scoped product endpoint: https://sap.github.io/spartacus-docs/loading-scopes/#using-default-product-scopes. Note: When writing this post, the docs for Spartacus 2.0 version are not yet published. In 2.0 the product_scopes config property was renamed to product.



来源:https://stackoverflow.com/questions/61595465/extending-product-model-in-spartacus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!