项目作者: brunchz

项目描述 :
A quick script to import Shopify pages, blogs and products using Axios and REST.
高级语言: JavaScript
项目地址: git://github.com/brunchz/Storecloner-Shopify.git
创建时间: 2021-02-15T09:42:25Z
项目社区:https://github.com/brunchz/Storecloner-Shopify

开源协议:

下载


Shopify Storecloner Script

This script uses the Shopify Admin API to clone the following resources:

1) Blogs
2) Pages
3) Products*

Installing

  1. $ npm install
  1. $ npm start

Prerequisites

This script requires a process.env file on the root folder to retreive the API Key and API Secret as shown below:

  1. apiKey_source = 'YOUR SOURCE API KEY'
  2. apiSecret_source = 'YOUR SOURCE API SECRET'
  3. apiKey_dest = 'YOUR DESTINATION API KEY'
  4. apiSecret_dest = 'YOUR DESTINATION API SECRET'
  5. sourceURL = 'YOUR SOURCE URL'
  6. destinationURL = 'YOUR DESTINATION URL'

See dotenv and how to get API keys and secrets for more info.

main.js

Using Axios, this script first sends a GET request to the endpoint https://{storeURL}.myshopify.com/admin/api/2021-01/{resource}.json (hardcoded into the URL parameter for now).

After successful retrieval all the resources, it sends a POST request to the endpoint https://{storeURL}.myshopify.com/admin/api/2021-01/{resource}.json. A successful POST request will respond with status code = 201.

Example of a GET resquest response:

  1. {
  2. blogs: [{
  3. id: 76612534453,
  4. handle: 'news-11',
  5. title: 'News',
  6. updated_at: '2021-02-15T04:45:09-05:00',
  7. feedburner_location: null,
  8. created_at: '2021-02-15T04:45:09-05:00',
  9. template_suffix: null,
  10. admin_graphql_api_id: 'gid://shopify/OnlineStoreBlog/76612534453'
  11. }
  12. ...
  13. }

This data is taken from the sourceURL and posted to the destURL in a forloop for the length of the resource array.

Example result:

  1. ====DUPLICATING PAGES====
  2. ===Fetching Pages===
  3. ===Fetching Pages===
  4. Page Data Fetched
  5. ===Posting 1 of 36 Pages===
  6. ===Posting 2 of 36 Pages===
  7. ===Posting 3 of 36 Pages===

Deleting resources

  1. npm run delete

Deletes all the resources on the destination store incase anything goes wrong and a reset is needed.

Edit which resources gets deleted by changing the args in deleteResource.js

  1. main(['pages','blogs']).then(console.log);

DISCLAIMER

I have found (rather embarassingly 🤦) that there exists shopify-api-node which basically eliminates the need for Axios, but rather creates an instance to do exactly what’s done here in cleaner code. I will be spending sometime reading the docs and porting over my code to implement this package instead.

Future Improvements

Future developments include embedding this script into a public Shopify app via Shopify App Bridge, allowing a user to import resources through a familar UI like Polaris.

*Shops that use Gempages need to import the theme.zip manually before importing the pages as the POST request is reliant on Gempage templates.

**There are some issues with variant images (undefined image_ids during posting) which results in an error: 422
To resolve this, I eliminated the image_ids from product variants.