当涉及到这类任务时,我通常会为自己编写一个快速的开发控制台脚本,它会为我完成这项工作,而不是依赖应用程序。
这是您可以在 shopify 管理页面的开发控制台中使用的脚本(只需复制/粘贴):
let productsArray = [];
// Recursive function that will grab all products from a collection
const requestCollection = (collectionId, url = `https://${window.location.host}/admin/api/2020-10/collections/${collectionId}/products.json?limit=250`) => {
fetch(url).then(async res => {
const link = res.headers.get('link');
const data = await res.json();
productsArray = [...productsArray, ...data.products];
if(link && link.match(/<([^\s]+)>;\srel="next"/)){
const nextLink = link.match(/<([^\s]+)>;\srel="next"/)[1];
requestCollection(collectionId, nextLink)
} else {
initDelete(productsArray)
}
})
}
// Get CSRF token or the request will require password
const getCSRFToken = () => fetch('/admin/settings/files',{
headers: {
"x-requested-with": "XMLHttpRequest",
"x-shopify-web": 1,
"x-xhr-referer": `https://${window.location.host}/admin/settings/files`
}
}).then(res => res.text()).then(res => {
const parser = new DOMParser();
const doc = parser.parseFromString(res, 'text/html');
return doc.querySelector('meta[name="csrf-token"]').getAttribute('content')
})
// The function that will start the deleting process
const initDelete = async (products) => {
const csrfToken = await getCSRFToken();
products.forEach(item => {
fetch(`https://${window.location.host}/admin/api/2020-10/products/${item.id}.json`, {
method: "delete",
credentials: 'include',
headers: {
"x-csrf-token": csrfToken,
"x-requested-with": "XMLHttpRequest",
"x-shopify-web": 1,
"x-xhr-referer": `https://${window.location.host}/admin/settings/files`
}
})
})
}
然后使用requestCollection(ADD_YOUR_COLLECTION_ID_HERE) 启动它。
为了澄清脚本,有3个主要功能:
-
requestCollection - 这处理从集合中抓取的产品。这是一个递归函数,因为我们不能同时抓取超过 250 个产品。
-
getCSRFToken - 这会获取 CSRF 令牌,因为大多数发布/更新/删除请求都需要它,否则它们将失败(我从文件页面获取)
-
initDelete - 此函数启动删除过程,我们将所有请求彼此堆叠而不等待,您可能希望等待每个请求,但即使您的浏览器崩溃,我认为重复该过程会更快,而不是然后等待每个请求完成。
如果您打算使用此脚本,请在使用前进行测试。创建一个包含一些产品的集合并针对它运行,以防出现问题。我已经对其进行了测试,它可以正常工作,但这是我在午夜后 10 分钟内编写的代码,可能存在问题。
请记住,此脚本将删除您在 requestCollection(1231254125) 方法中指定的集合中的所有产品。
PS:所有这些都可以使用私有应用程序以及将产品范围设置为读/写,使用您选择的后端语言来完成。主要区别在于您不需要 CSRF 令牌和我在上面设置的大多数标头。但我喜欢不需要你大刀阔斧的快速解决方案。