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

[Bug]: Custom Fulfillment provider service unexpected behavior #10002

Closed
malvere opened this issue Nov 9, 2024 · 9 comments
Closed

[Bug]: Custom Fulfillment provider service unexpected behavior #10002

malvere opened this issue Nov 9, 2024 · 9 comments

Comments

@malvere
Copy link

malvere commented Nov 9, 2024

Package.json file

{
  "name": "medusa-starter-default",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "latest",
    "@medusajs/cli": "latest",
    "@medusajs/framework": "latest",
    "@medusajs/medusa": "latest",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "axios": "^1.7.7",
    "pg": "^8.13.0"
  },
  "devDependencies": {
    "@medusajs/test-utils": "latest",
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.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"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v22.9.0

Database and its version

PostgreSQL 16

Operating system name and version

macOS 14.6.1

Browser name

Safari/Firefox/Chrome

What happended?

Custom fulfillment provider service implementation not working as expected, canCalculate never called

Example service implementation as stated in https://docs.medusajs.com/resources/references/fulfillment/provider

service.ts:

import {
    AbstractFulfillmentProviderService,
} from "@medusajs/framework/utils";
import { FulfillmentOption, Logger } from "@medusajs/framework/types";
import { CustomSDK } from "./sdk"

type InjectedDependencies = {
    logger: Logger
}

type Options = {
    account: string,
    secret: string,
    from_location: string
}

class MyProviderService extends AbstractFulfillmentProviderService {
    static identifier: string = "my-delivery";

    protected logger_: Logger
    protected options_: Options
    protected sdk: CustomSDK

    private token: any
    private from_location: string

    constructor(
        { logger }: InjectedDependencies,
        options: Options
    ) {
        // @ts-ignore
        super(...arguments)
        this.logger_ = logger
        this.sdk = new CustomSDK()
        this.options_ = options
        this.from_location = options.from_location

    }

    async initialize() {
        this.logger_.debug("Initialized My Fulfilment")

        this.token = await this.sdk.getToken({
            account: this.options_.account,
            secret: this.options_.secret,
        });
    }

    async getFulfillmentOptions(): Promise<FulfillmentOption[]> {
        return [
            {
                id: "my-fulfillment",
            },
        ];
    }

    async validateFulfillmentData(optionData: Record<string, unknown>, data: Record<string, unknown>, context: Record<string, unknown>): Promise<any> {
        this.logger_.debug(`validateFulfillmentData. \nData: ${data}`)
        this.logger_.debug(`ptionData: ${optionData}`)
        this.logger_.debug(`context: ${context}`)
        return {
            ...data
        }
    }

    async validateOption(data: Record<string, unknown>): Promise<boolean> {
        this.logger_.debug(`validateOption. \nData: ${data}`)
        console.log("validateOption")
        return true
    }

    async canCalculate(data: any): Promise<boolean> {
        this.logger_.debug(`canCalculate. \nData: ${data}`)
        console.log(data)
        return true
    }

    async calculatePrice(optionData: Record<string, unknown>, data: Record<string, unknown>, cart: any): Promise<number> {
        this.logger_.debug(`calculatePrice. \nData: ${data}`)
        this.logger_.debug(`optionData: ${optionData}`)
        this.logger_.debug(`cart: ${cart}`)
        console.log(data)
        return 1000
    }
}
export default MyProviderService;

index.ts:

import { ModuleProvider, Modules } from "@medusajs/framework/utils";
import MyProviderService from "./service";

export default ModuleProvider(Modules.FULFILLMENT, {
    services: [MyProviderService]
})

Expected behavior

Shipping price should be explicitly set to 1000. Or any number returned from canCalculate method

Actual behavior

If Shipping option is created and prices are left blank in shipping option's settings then there is an error

error:   Shipping options with IDs so_01JC9EHG28WM656FBNVT6JVHRF do not have a price

If price is provided in Shipping option settings via Admin UI, then prices in those options will override anything returned by canCalculate

Link to reproduction repo

https://github.com/medusajs/medusa

@olivermrbl
Copy link
Contributor

@malvere, thanks for the report. We currently don't support calculated shipping option prices. However, we are tracking this issue internally and will introduce it within a month or two.

@HarleySalas
Copy link

@malvere, thanks for the report. We currently don't support calculated shipping option prices. However, we are tracking this issue internally and will introduce it within a month or two.

Wish I had known that before spending the time to write an SDK for a fulfillment provider.

You guys really should have some sort of resource somewhere that outlines what is currently supported and what isn't.

I also believe you should add metadata or something to easily be able to add extra data to shipping options, per warehouse/inventory location, which will then be available in the fulfillment service props. The from_location will be different for every inventory location, which matters for calculating pricing.

@mawoka-myblock
Copy link

I'm joining this club here. To clarify: You're providing an SDK or the option to create a shipping option with dynamic pricing which isn't yet implemented? I'd really wish to read a disclaimer like:

"Here's the SDK, though it's not functional in Medusa v2"

This really sucks and not even providing a clear error. I mean, there's a Guide in the docs on how to create a dynamic shipping option. That's not how guides work IMO. Please remove that section or add a disclaimer as mentioned above.

@c4iarx
Copy link

c4iarx commented Dec 1, 2024

Any update on this @olivermrbl ?

@olivermrbl
Copy link
Contributor

Hey guys – we will be adding support for this this week or next.

To help us understand the different use cases, could I get you to share what you expect to use to calculate prices for shipping options? For example, products, variant, addresses, etc.

We will need to pass this data down to the provider.

@mawoka-myblock
Copy link

Sounds good. I expect it to take all the items in the shopping cart as an argument and the shipping address. Off of that, it should be able to calculate the price.

@olivermrbl
Copy link
Contributor

@mawoka-myblock, thanks for sharing. That's exactly what we will be adding initially. You can track the PR here.

@mawoka-myblock
Copy link

Awesome, subscribed for updates, thanks! But please don't forget to adjust the docs accordingly to your changes!

@dooke
Copy link

dooke commented Dec 16, 2024

+1 here i would like to have item (product properties) to calculate shipping based on product weight (maybe length width height in future for volume)
@mawoka-myblock The doc you talked about do you have link (not easy to find)

Wait for news!

@linear linear bot closed this as completed Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants