Creating Reshipments in Shopify with Data Models

Need the solution? Code is posted down below

I once received a super, deeply nested response from a server that was impossible to parse while on a project. Not having access to the API to reengineer the endpoint’s response, I couldn’t think of a solution. Fast forward to today and I’m using data models to build out server responses like a boss.

Take for example creating a reshipment for customer using the Shopify API. According to the documentation, in order to create a reshipment, your app should create a duplicate order and have the same data found in the original order. But the catch is that a discount needs to be applied to the shipping so that the customer is not charged.

Using a data model comes in very handy since it can assign values and quickly map the incoming request to that model.

Looking at the gist above, I used a data model to assign the discount equal to the subtotal. This ensures that the customer is not charged for the reshipment. If I tried to do this in the http request, it would’ve been such a headache.

The other class that is handy is the ShippingLines class. This takes the amount the customer paid for each item and applies a discount to the shipping costs.

The last piece of the puzzle is to create a new instantiation of the Order model and map the values the front end sends the server to it. Look at line 7. That is where the values sent to the server are being mapped to the new Order model

Lastly, send this order to the Shopify create order REST API endpoint, and your customer will have a new order reshipped to them.

If you develop using the Shopify API, I hope you find this helpful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s