评级扩展教程

本教程将指导您使用Adobe Commerce as a Cloud Service和AI辅助开发工具为Adobe App Builder构建产品评级扩展。

在开始之前,请完成先决条件

验证先决条件

验证是否安装了以下必备组件:

# Check Node.js version (should be 22.x.x)
node --version

# Check npm version (should be 9.0.0 or higher)
npm --version

# Check Git installation
git --version

# Check Bash shell installation
bash --version

如果前面的任何命令未返回预期结果,请参阅先决条件获取指导。

扩展开发

此部分将指导您使用AI辅助开发工具为Adobe Commerce as a Cloud Service开发评级扩展。

  1. 导航到​Cursor > Settings > Cursor Settings > Tools & MCP,并验证commerce-extensibility工具集是否已启用且未出现错误。 如果看到错误,请关闭和打开工具集。

    Cursor IDE设置显示MCP商务扩展工具集已启用 {width="600" modal="regular"}

    note note
    NOTE
    使用人工智能辅助开发工具时,预期代码和代理生成的响应会发生自然变化。
    如果您遇到任何代码问题,始终可以请求代理帮助您对其进行调试。
  2. 如果有任何文档添加到光标上下文中,请禁用它:

    • 导航到​Cursor > 设置 > Cursor设置 > 索引和文档,并删除列出的任何文档。

    文档列表为空的游标索引和文档设置 {width="600" modal="regular"}

  3. 为产品评级扩展生成代码:

    • 在“光标聊天”窗口中,选择​ 代理 ​模式。
    • 输入以下提示:
    code language-shell-session
    Implement an Adobe Commerce as a Cloud Service extension to handle Product Ratings.
    
    Implement a REST API to handle GET ratings requests.
    
    GET requests will have to support the following query parameters:
    
    sku -> product SKU
    
    note note
    NOTE
    如果代理请求搜索文档,请允许搜索。
  4. 准确地回答座席的问题以帮助其生成最佳代码。

    在代理模式下光标聊天窗口,已输入扩展提示 {width="600" modal="regular"} AI代理询问有关扩展要求的问题 {width="600" modal="regular"}

  5. 使用以下示例文本回答座席的问题,以设置随机评级数据:

    code language-shell-session
    Yes, this headless extension is for Adobe Commerce as a Cloud Service storefront,
    but we do not need any authentication for the GET API because guest users should be able to use it on the storefront.
    
    This extension is called directly from the storefront, no async invocation, such as events or webhooks, is required.
    
    Start with just the GET API for now, we will implement other CRUD operations at a later time.
    
    We do not need a DB or storage mechanism right now, just return random ratings data between 1 and 5 and a ratings count between 1 and 1000.
    
    The API should only return the average rating for the product and the total number of ratings.
    We do not need to add tests right now.
    

    代理将创建一个requirements.md文件,用作实现的真实来源。

    AI代理创建的 Requirements.md文件及实现详细信息 {width="600" modal="regular"}

  6. 查看requirements.md文件并验证计划。

    如果一切看起来都正确,请指示代理移至​阶段2 — 架构计划

  7. 查看体系结构计划。

  8. 指示代理继续生成代码。

    代理程序会生成必要的代码并提供详细的摘要,其中包含后续步骤。

    分级API的AI代理2期体系结构计划 {width="600" modal="regular"} 生成的代码文件和结构的摘要 {width="600" modal="regular"} AI代理提供测试和部署的后续步骤 {width="600" modal="regular"}

本地测试

  1. 请求代理帮助您在本地测试代码。

    code language-shell-session
    Test the ratings API locally on a dev server using cURL.
    
  2. 按照代理的说明操作,并确认API在本地工作。

    用于本地API测试的 AI代理说明 {width="600" modal="regular"}

    终端显示成功的本地API测试结果(cURL {width="600" modal="regular"})

部署扩展

  1. 验证生成的代码后,使用以下提示部署扩展:

    code language-shell-session
    Deploy the ratings API.
    

    代理在部署之前执行部署前就绪性评估。

    AI代理部署前准备情况评估核对清单 {width="600" modal="regular"}

  2. 如果对评估结果有信心,请指示代理继续部署。

    代理使用MCP工具包自动验证、构建和部署。

    MCP工具包验证生成和部署过程 {width="600" modal="regular"}

部署后

您可以在将API集成到店面之前对其进行测试。 代理应提供新操作的位置和测试策略。

具有已部署的操作URL和测试命令的 AI代理测试策略 {width="600" modal="regular"}

您还可以在终端中使用cURL手动测试API:

curl -s "https://<your-site>.adobeioruntime.net/api/v1/web/ratings/ratings?sku=TEST-SKU-123"

终端显示已部署的分级API的cURL测试成功 {width="600" modal="regular"}

与Edge Delivery Services集成

要将评级API与由Adobe Commerce提供支持的Edge Delivery Services店面集成,请要求代理创建具有评级API要求的服务合同:

Create a service contract for the ratings api that I can pass on to the storefront agent. Name it RATINGS_API_CONTRACT.md

AI代理为店面集成创建服务合同文件 {width="600" modal="regular"}

带有端点和响应详细信息的Rating API合同Markdown文件 {width="600" modal="regular"}

后续步骤

现在您有了评级API合同,就可以开始构建评级扩展的店面(前端)部分了。

recommendation-more-help
29fd6b16-22d9-4d45-b5c9-95c2f0341493