@csbeker/medusa-product-attributes
v2.4.6
Published
A plugin for adding attributes support to MedusaJS products
Downloads
708
Maintainers
Readme
Medusa Product Attributes Plugin
[!NOTE] Este plugin es un fork y continúa el desarrollo del repositorio original de Nicolas Gorga. Se agregan mejoras, soporte y nuevas funcionalidades sobre la base de su trabajo.
Create global (category specific) product attributes on your Medusa commerce application.
Compatibility
This plugin is compatible with versions >= 2.6.1 of @medusajs/medusa.
Progress
All the progress is recorded in CHANGELOG.md and so far, it is solely focused on backend (you can use the functionallities via API calls)
Roadmap
- Create Admin panel widgets link attribute values to products
Prerequisites
How to Install
1. Run the following command in the directory of the Medusa backend using your package manager (for example for npm):
npm install @nicogorga/medusa-product-attributes2. In medusa-config.ts add the following to the plugins array in your project config:
module.exports = defineConfig({
projectConfig: {
// ...
},
plugins: [
{
resolve: `@nicogorga/medusa-product-attributes`,
options: {}
}
]
})3. In your already existent or new createProductsWorkflow.productsCreated hook handler, import productsCreatedHookHandler and call it like following. This is important as it's what links new products to the requested attribute values (if any):
import { createProductsWorkflow } from "@medusajs/medusa/core-flows";
import { productsCreatedHookHandler } from "@nicogorga/medusa-product-attributes/utils/products-created-handler"
createProductsWorkflow.hooks.productsCreated(
async ({ products, additional_data }, { container }) => {
const links = await productsCreatedHookHandler({ products, additional_data, container })
// Your own logic (if any). You can return 'links' to your compensation function, to dismiss the links if an error occurs
}
)4. In your already existent or new updateProductsWorkflow.productsUpdated hook handler, import productUpdatedHookHandler and call it like following. This is important as it's what updated the porduct attributes, with the requested attribute values (if any):
import { updateProductsWorkflow } from "@medusajs/medusa/core-flows";
import { productsUpdatedHookHandler } from "@nicogorga/medusa-product-attributes/utils/products-updated-handler"
updateProductsWorkflow.hooks.productsUpdated(
async ({ products, additional_data }, { container }) => {
const links = await productsUpdatedHookHandler({ products, additional_data, container })
// Your own logic (if any). You can return 'links' to your compensation function, to dismiss the links if an error occurs
}
)API Endpoints
Store Endpoints
Get Product Attributes
GET /store/plugin/attributes/products/[product_id]Retrieves all attributes associated with a specific product, including their values.
Response Format:
{
"attributes": [
{
"id": "attr_xxx",
"name": "Caracteristicas",
"ui_component": "sortable_list",
"is_variant_defining": true,
"value": "[\"Feature 1\",\"Feature 2\",\"Feature 3\"]"
}
]
}Fields:
id: Unique identifier for the attributename: Display name of the attributeui_component: Type of UI component used in admin (e.g., "sortable_list", "text", "select")is_variant_defining: Whether this attribute defines product variantsvalue: The attribute value. For Sortable List components, this is a JSON-serialized array of strings
Usage Example (Frontend):
// Fetch product attributes
const response = await fetch(`/store/plugin/attributes/products/${productId}`)
const { attributes } = await response.json()
// Parse Sortable List values
const characteristicsAttr = attributes.find(attr => attr.ui_component === "sortable_list")
if (characteristicsAttr?.value) {
const features = JSON.parse(characteristicsAttr.value) // ["Feature 1", "Feature 2", ...]
}Additional Resources
- medusa-custom-attributes v1 Props to Viktor Holik, the creator, for his work as it is a great reference and has proved to be open to help
- Github Discussions
