Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate from magento #35

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .cursorrules
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
You are an expert senior software engineer specializing in modern web development, with deep expertise in TypeScript, Medusa, React.js, and TailwindCSS.

## Medusa Rules

### General Rules

- Don't use type aliases when importing files.
- When throwing errors, always throw `MedusaError`.

### Workflow Rules

- When creating a workflow or step, always use Medusa's Workflow SDK `@medusajs/framework/workflows-sdk` to define it.
- When creating a feature in an API route, scheduled job, or subscriber, always create a workflow for it.
- When creating a workflow, always create a step for it.
- In workflows, use `transform` for any data transformation.
- In workflows, use `when` to define conditions.
- Don't use `await` when calling steps.

### Data Model Rules

- Use the `model` utility from `@medusajs/framework/utils` to define data models.
- Data model variables should be camelCase. Data model names as passed to `model.define` should be snake case.
- When adding an `id` field to a data model, always make it a primary key with `.primaryKey()`.
- Data model fields should be snake case.

### Service Rules

- When create a service, always make methods async.
- If a module has data models, make the service extend `MedusaService`.

### Admin Customization Rules

- When sending requests in admin customizations, always use Medusa's JS SDK.
- Use TailwindCSS for styling.
26 changes: 26 additions & 0 deletions migrate-from-magento/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/dist
.env
.DS_Store
/uploads
/node_modules
yarn-error.log

.idea

coverage

!src/**

./tsconfig.tsbuildinfo
medusa-db.sql
build
.cache

.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

.medusa
145 changes: 145 additions & 0 deletions migrate-from-magento/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Medusa v2 Example: Migrate Data from Magento to Medusa

This directory holds the code for the [Migrate Magento to Medusa Guide](https://docs.medusajs.com/resources/examples/guides/custom-item-price).

You can either:

- [install and use it as a plugin in the Medusa application](#installation);
- or [copy its source files into an existing Medusa application, without using them as a plugin](#copy-into-existing-medusa-application).

## Prerequisites

- [Node.js v20+](https://nodejs.org/en/download)
- [Git CLI](https://git-scm.com/downloads)
- [PostgreSQL](https://www.postgresql.org/download/)
- Magento server with admin credentials.

## Installation

> Learn more about building and developing with plugins in [this documentation](https://docs.medusajs.com/learn/fundamentals/plugins/create).

1. Clone the repository and change to the `migrate-from-magento` directory:

```bash
git clone https://github.com/medusajs/examples.git
cd examples/migrate-from-magento
```

2\. Install dependencies:

```bash
yarn install # or npm install
```

3\. Publish to local registry:

```bash
npx medusa plugin:publish
```

4\. Build plugin:

```bash
npx medusa plugin:build
```

5\. In a Medusa application, install the plugin from the local registry:

```bash
npx medusa plugin:add migrate-from-magento
```

6\. Add the plugin to `medusa-config.ts`:

```ts
module.exports = defineConfig({
// ...
plugins: [
{
resolve: "migrate-from-magento",
options: {
baseUrl: process.env.MAGENTO_BASE_URL,
username: process.env.MAGENTO_USERNAME,
password: process.env.MAGENTO_PASSWORD,
migrationOptions: {
imageBaseUrl: process.env.MAGENTO_IMAGE_BASE_URL,
}
},
},
],
})
```

7\. Set the following environment variables:

```bash
MAGENTO_BASE_URL=https://magento.example.com
MAGENTO_USERNAME=admin
MAGENTO_PASSWORD=password
MAGENTO_IMAGE_BASE_URL=https://magento.example.com/pub/media/catalog/product
```

Where:

- `MAGENTO_BASE_URL`: The base URL of the Magento server. It can also be a local URL, such as `http://localhost:8080`.
- `MAGENTO_USERNAME`: The username of a Magento admin user to authenticate with the Magento server.
- `MAGENTO_PASSWORD`: The password of the Magento admin user.
- `MAGENTO_IMAGE_BASE_URL`: The base URL to use for product images. Magento stores product images in the `pub/media/catalog/product` directory, so you can reference them directly or use a CDN URL. If the URLs of product images in the Medusa server already have a different base URL, you can omit this option.

## Copy into Existing Medusa Application

You can also copy the source files into an existing Medusa application, which will add them not as a plugin, but as standard Medusa customizations.

1. Copy the content of the following directories:

- `src/admin`
- `src/api/admin` and `src/api/middlewares.ts`
- `src/jobs`
- `src/modules/magento`
- `src/modules/subscribers`
- `src/workflows`

2. Add the Magento Module to `medusa-config.ts`:

```ts
module.exports = defineConfig({
// ...
modules: [
{
resolve: "./src/modules/magento",
options: {
baseUrl: process.env.MAGENTO_BASE_URL,
username: process.env.MAGENTO_USERNAME,
password: process.env.MAGENTO_PASSWORD,
migrationOptions: {
imageBaseUrl: process.env.MAGENTO_IMAGE_BASE_URL,
}
}
},
]
})
```

3. Set the following environment variables:

```bash
MAGENTO_BASE_URL=https://magento.example.com
MAGENTO_USERNAME=admin
MAGENTO_PASSWORD=password
MAGENTO_IMAGE_BASE_URL=https://magento.example.com/pub/media/catalog/product
```

Where:

- `MAGENTO_BASE_URL`: The base URL of the Magento server. It can also be a local URL, such as `http://localhost:8080`.
- `MAGENTO_USERNAME`: The username of a Magento admin user to authenticate with the Magento server.
- `MAGENTO_PASSWORD`: The password of the Magento admin user.
- `MAGENTO_IMAGE_BASE_URL`: The base URL to use for product images. Magento stores product images in the `pub/media/catalog/product` directory, so you can reference them directly or use a CDN URL. If the URLs of product images in the Medusa server already have a different base URL, you can omit this option.

## Test it Out

To test out that the customizations are working, open the Medusa Admin at `http://localhost:9000/app`. You'll find a "Migrate Magento" sidebar item. Click on it, and you can trigger a migration of products and categories.

## More Resources

- [Medusa Documentatin](https://docs.medusajs.com)
76 changes: 76 additions & 0 deletions migrate-from-magento/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "migrate-from-magento",
"version": "0.0.1",
"description": "A starter for Medusa plugins.",
"author": "Medusa (https://medusajs.com)",
"license": "MIT",
"files": [
".medusa/server"
],
"exports": {
"./package.json": "./package.json",
"./workflows": "./.medusa/server/src/workflows/index.js",
"./.medusa/server/src/modules/*": "./.medusa/server/src/modules/*/index.js",
"./providers/*": "./.medusa/server/src/providers/*/index.js",
"./*": "./.medusa/server/src/*.js"
},
"keywords": [
"medusa",
"plugin",
"medusa-plugin-other",
"medusa-plugin",
"medusa-v2"
],
"scripts": {
"build": "medusa plugin:build",
"dev": "medusa plugin:develop",
"prepublishOnly": "medusa plugin:build"
},
"devDependencies": {
"@medusajs/admin-sdk": "2.5.0",
"@medusajs/cli": "2.5.0",
"@medusajs/framework": "2.5.0",
"@medusajs/icons": "2.5.0",
"@medusajs/medusa": "2.5.0",
"@medusajs/test-utils": "2.5.0",
"@medusajs/ui": "4.0.4",
"@mikro-orm/cli": "6.4.3",
"@mikro-orm/core": "6.4.3",
"@mikro-orm/knex": "6.4.3",
"@mikro-orm/migrations": "6.4.3",
"@mikro-orm/postgresql": "6.4.3",
"@swc/core": "1.5.7",
"@types/node": "^20.0.0",
"@types/react": "^18.3.2",
"@types/react-dom": "^18.2.25",
"awilix": "^8.0.1",
"pg": "^8.13.0",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"ts-node": "^10.9.2",
"typescript": "^5.6.2",
"vite": "^5.2.11",
"yalc": "^1.0.0-pre.53"
},
"peerDependencies": {
"@medusajs/admin-sdk": "2.5.0",
"@medusajs/cli": "2.5.0",
"@medusajs/framework": "2.5.0",
"@medusajs/icons": "2.5.0",
"@medusajs/medusa": "2.5.0",
"@medusajs/test-utils": "2.5.0",
"@medusajs/ui": "4.0.3",
"@mikro-orm/cli": "6.4.3",
"@mikro-orm/core": "6.4.3",
"@mikro-orm/knex": "6.4.3",
"@mikro-orm/migrations": "6.4.3",
"@mikro-orm/postgresql": "6.4.3",
"awilix": "^8.0.1",
"pg": "^8.13.0"
},
"engines": {
"node": ">=20"
},
"dependencies": {}
}
31 changes: 31 additions & 0 deletions migrate-from-magento/src/admin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Admin Customizations

You can extend the Medusa Admin to add widgets and new pages. Your customizations interact with API routes to provide merchants with custom functionalities.

## Example: Create a Widget

A widget is a React component that can be injected into an existing page in the admin dashboard.

For example, create the file `src/admin/widgets/product-widget.tsx` with the following content:

```tsx title="src/admin/widgets/product-widget.tsx"
import { defineWidgetConfig } from "@medusajs/admin-sdk"

// The widget
const ProductWidget = () => {
return (
<div>
<h2>Product Widget</h2>
</div>
)
}

// The widget's configurations
export const config = defineWidgetConfig({
zone: "product.details.after",
})

export default ProductWidget
```

This inserts a widget with the text “Product Widget” at the end of a product’s details page.
Loading