Last active
June 15, 2023 19:47
-
-
Save neodigm/86a12ef18375b8ad59ea80fefa1aeecb to your computer and use it in GitHub Desktop.
Shopify custom collection of products without images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.