Skip to content

Instantly share code, notes, and snippets.

@neodigm
Last active June 15, 2023 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neodigm/86a12ef18375b8ad59ea80fefa1aeecb to your computer and use it in GitHub Desktop.
Save neodigm/86a12ef18375b8ad59ea80fefa1aeecb to your computer and use it in GitHub Desktop.
Shopify custom collection of products without images
import fetch from 'node-fetch';
// import util from "util";
import jProds from "./json/chps_shpy_prods_images.json" assert { type: "json" };
let oHeaders = {'Content-Type': 'application/json', 'X-shopify-Access-Token': 'qqq'}
const SBASE = "https://qqq.myshopify.com/admin/api/2023-04/"
const SNOIMAGECOL = "449524269336"
let aTick = [];
let genTick = function(){
jProds.forEach( async function( oPrd, nDx ){
if( oPrd.images.length == 0 ) aTick.push( {"id":oPrd.id, "done":false } )
})
}
let addToCollection = async function(){
let oTick = aTick.filter( ( oT )=>{
return ( !oT.done )
})[ 0 ]
if( !oTick ) return
oTick.done = true
console.log( " ~~~ ~~~ | " + oTick.id + " | " + oTick.done )
let sAddProd = '{"custom_collection":{"id":' + SNOIMAGECOL + ',"collects":[{"product_id":' + oTick.id + '}]}}'
const response = await fetch("https://qqq.myshopify.com/admin/api/2023-04/custom_collections/" + SNOIMAGECOL + ".json", {
body: sAddProd, method: 'put', headers: oHeaders
}).then( ( r ) =>{ console.log( " ~~~ ~~~ res status || " , r.status ) })
setTimeout( ()=>{ addToCollection() }, 1200 )
}
async function init(){
genTick()
await addToCollection()
console.log( " ~~~ aTick | " + aTick.length )
}
init()
@neodigm
Copy link
Author

neodigm commented Jun 15, 2023

This script uses the Shopify Admin API to update (put) a custom collection with products that do not have images. The process is throttled to fire 1 API call per second.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment