是否可以在不传递 catalogType 的情况下检索产品?

Is that possible to retrieve products without passing catalogType?

我需要在不知道目录类型的情况下通过代码获取产品。是否可以在不传递 catalogType 的情况下检索产品?

下面是我试过的代码片段:

@Resource
private ProductDao productDao;
    
@Resource
    private CatalogVersionService catalogVersionService;


List<ProductModel> getProductsByCode(String code) {

    CatalogVersionModel catalogVersionModel = new CatalogVersionModel();
    catalogVersionModel.setVersion("Online");
    catalogVersionService.addSessionCatalogVersion(catalogVersionModel);
    List<ProductModel> productModels = productDao.findProductsByCode(code);

}

下面是我得到的异常:

{
    "errors": [
        {
            "message": "model CatalogVersionModel (<unsaved>) cannot be serialized due to being modified, new or removed",
            "type": "FlexibleSearchError"
        }
    ]
}

我可以知道如何解决上述问题吗?

您需要指定目录,因为可能有多个目录,并且所有这些目录中可能存在相同的产品。

当您在 SAP Commerce (hybris) 中创建 product/variant 时,您必须将其附加到目录。

一个目录(CatalogModel)也有一个版本(通常是staged或者online),对象叫做一个CatalogVersionModel

当你想检索一个product/variant时,你必须指明CatalogVersionModel,因为产品代码不是在数据库中检索产品的唯一键(你可以检查类型“产品" 在后台查看 XML 窗格,代码和目录版本都具有值 unique="true")

现在你的代码中有几个问题。

  • 您不应创建目录版本,而应使用服务检索它(参见 DefaultCatalogVersionService
  • 您应该使用服务来检索您的产品(参见 DefaultProductService

productService 实现中,您会发现两种方法 getProductForCode

  • 一个只有sku代码作为参数
  • 一个以sku代码和catalogVersion作为参数

第一种方法实际上看起来像你想要的方法,但实际上,它在你的会话中使用了catalogVersion。如果您 运行 您的代码在 groovy 中,或者如果您 运行 您的代码在 Java 中来自您的电子商务网站,您的会话将会不同。

你可以在下面找到这个方法的注释

Returns the Product with the specified code. As default the search uses the current session user, the currentsession language and the current active catalog versions (which are stored at the session in the attribute SESSION_CATALOG_VERSIONS).For modifying the search session context see FlexibleSearchQuery.