Category

Other

Version

0.1.4

Last updated

Sep 6, 2024, 18:06:20 PM4 months ago

Medusa Recommend Product Plugin

The Medusa Recommend Product Plugin is a powerful tool for managing product recommendations within your Medusa e-commerce platform. This plugin allows you to add, view, and manage product recommendations easily, enhancing the user experience by suggesting related or popular products to your customers.

Table of Contents

  1. Installation
  2. Usage
  3. API Reference
  4. Error Handling
  5. License

Installation

To install the plugin, use the following command:
npm install medusa-recommend-product-plugin

Usage

Register the Plugin

To activate the plugin, you need to add it to the list of plugins in your Medusa configuration file (Copy to clipboardmedusa-config.js).
  1. Open Copy to clipboardmedusa-config.js.
  2. Locate the Copy to clipboardplugins array.
  3. Add the plugin configuration as shown below:
const plugins = [
// other plugins
{
resolve: "medusa-recommend-product-plugin",
options: {
enableUI: true, // Enables the admin UI for managing recommendations
},
},
];

Run Migrations

After registering the plugin, run the migrations to set up the necessary database tables:
npx medusa migrations run

Admin Panel Features

Once the plugin is set up and migrations are run, you can manage product recommendations directly from the Medusa admin panel:
  • View Recommendations: On the product details page, you can view the list of current recommendations.
  • Add New Recommendations: Easily add new product recommendations through a simple interface.

API Reference

Store API

The Store API provides endpoints for retrieving product recommendations for customers.

Get Product Recommendations

Retrieve the list of recommendations for a specific product.
  • URL: Copy to clipboard/api/store/products/{productId}/recommendations
  • Method: Copy to clipboardGET
  • Content-Type: Copy to clipboardapplication/json
Query Parameters
NameTypeDescription
Copy to clipboardproductIdstringThe ID of the product for which to retrieve recommendations.
Response
NameTypeDescription
Array of RecommendationsobjectList of recommended products.
Example Request
curl -X GET "https://your-medusa-url.com/api/store/products/123/recommendations"
Example Response
[
[
{
"id": "recommendation_.....",
"recommendedProduct": [{
"id": "prod1",
"name": "Product 1",
"description": "This is product 1",
...
}],
"forProductId":"product_1"
},
]
]

Admin API

The Admin API provides endpoints for managing product recommendations from the admin panel.

Create Product Recommendations

Create new recommendations for a specific product.
  • URL: Copy to clipboard/api/admin/recommendations
  • Method: Copy to clipboardPOST
  • Content-Type: Copy to clipboardapplication/json
Request Body
NameTypeDescription
Copy to clipboardforProductIdstringThe ID of the product to add recommendations for.
Copy to clipboardrecommendedProductsstring[]A list of product IDs to recommend.
Example Request
curl -X POST "https://your-medusa-url.com/api/admin/recommendations" \
-H "Content-Type: application/json" \
-d '{
"forProductId": "prod1",
"recommendedProducts": ["prod2", "prod3"]
}'
Example Response
{
"data": [
{
"id": "recommendation_.....",
"recommendedProduct": [{
"id": "prod1",
"name": "Product 1",
"description": "This is product 1",
...
}],
"forProductId":"product_1"
},
]
}

Delete Product Recommendations

Delete specific recommendations for a product.
  • URL: Copy to clipboard/api/admin/recommendations
  • Method: Copy to clipboardDELETE
  • Content-Type: Copy to clipboardapplication/json
Request Body
NameTypeDescription
Copy to clipboardrecommendationIdsstring[]The list of recommendation IDs to delete.
Example Request
curl -X DELETE "https://your-medusa-url.com/api/admin/recommendations" \
-H "Content-Type: application/json" \
-d '{
"recommendationIds": ["rec1", "rec2"]
}'
Example Response
{
"success": true
}

Error Handling

All API responses include standardized error handling to help you debug and handle errors effectively.

Error Response Format

Error responses are structured in the following format:
{
"message": "Error message describing the issue.",
"code": "Error code",
"errors": [
{
"field": "Field that caused the error",
"message": "Detailed message about the error"
}
],
"payload": {
// Additional error-related data, if applicable
}
}

Common Error Codes

  • Copy to clipboardINTERNAL_SERVER_ERROR: An unexpected error occurred on the server.
  • Copy to clipboardVALIDATION_ERROR: Input data failed validation.
  • Copy to clipboardBAD_REQUEST: The request was malformed or invalid.
  • Copy to clipboardNOT_FOUND: The requested resource was not found.
  • Copy to clipboardUNPROCESSABLE_ENTITY_ERROR: The server understood the request but could not process it.

Example Error Messages

  • Copy to clipboardrecommendationAlreadyExist: "This product already has a recommendation associated with the recommended product."
  • Copy to clipboardrecommendationNotFound: "Recommendation not found for the provided ID."
  • Copy to clipboardnoDuplicateProdRecommendation: "You cannot create multiple recommendations for the same product with the same recommended product."

License

This plugin is licensed under the MIT License. For more details, refer to the LICENSE file.

Build your own plugins

Develop your own plugins with our API to speed up your processes.

Make your plugin available via npm for it to be shared in our Plugin Library with the broader Medusa community.