如何删除匹配 collection 的所有产品 - Shopify
How to delete all products matching a collection - Shopify
我在 Shopify 的 collection 中有这么多产品(超过 5 万种产品),我需要将它们全部删除,有什么方法可以自动化吗?我在网上只能找到“批量编辑工具”,这是我见过的最没用的东西,一次只能抓取50个产品。
我已经尝试使用 CSV 导出文件自动执行脚本来更新行,但是导入 20K 产品需要 6 个多小时。另外,由于标题和句柄中有主题标签,它显然不会出于某种原因覆盖产品。所以我不能再使用存档了...
有没有人 运行 解决了这个问题并找到了解决方案?
谢谢!
当涉及到这类任务时,我通常会为自己编写一个快速的开发控制台脚本,它会为我完成这项工作,而不是依赖于应用程序。
这是一个脚本,您可以在 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
- 这处理从 collection 抓取的产品。这是一个递归函数,因为我们不能同时抓取超过 250 个产品。
getCSRFToken
- 这会获取 CSRF 令牌,因为大多数 post/update/delete 请求都需要它,否则它们会失败(我从文件页面获取它)
initDelete
- 此函数启动删除过程,我们将所有请求堆叠在一起而不等待,您可能希望等待每个请求,但即使您使浏览器崩溃,我认为它也会重复该过程比等待每个请求完成更快。
如果您打算使用此脚本,请在使用前对其进行测试。创建一个包含一些产品的 collection 并针对它创建 运行,以防出现问题。我已经在我这边测试过了,它可以工作,但这是我在午夜后 10 分钟内编写的代码,那里可能会有问题。
请记住,此脚本将删除您在 requestCollection(1231254125)
方法中指定的 collection 中的所有产品。
PS: 所有这些都可以使用私人应用程序以及产品范围设置为 read/write,使用 back-end 您选择的语言。主要区别在于您不需要 CSRF 令牌和我在上面设置的大部分 headers。但我喜欢不需要你拿出大手笔的快速解决方案。
我在 Shopify 的 collection 中有这么多产品(超过 5 万种产品),我需要将它们全部删除,有什么方法可以自动化吗?我在网上只能找到“批量编辑工具”,这是我见过的最没用的东西,一次只能抓取50个产品。
我已经尝试使用 CSV 导出文件自动执行脚本来更新行,但是导入 20K 产品需要 6 个多小时。另外,由于标题和句柄中有主题标签,它显然不会出于某种原因覆盖产品。所以我不能再使用存档了...
有没有人 运行 解决了这个问题并找到了解决方案?
谢谢!
当涉及到这类任务时,我通常会为自己编写一个快速的开发控制台脚本,它会为我完成这项工作,而不是依赖于应用程序。
这是一个脚本,您可以在 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
- 这处理从 collection 抓取的产品。这是一个递归函数,因为我们不能同时抓取超过 250 个产品。getCSRFToken
- 这会获取 CSRF 令牌,因为大多数 post/update/delete 请求都需要它,否则它们会失败(我从文件页面获取它)initDelete
- 此函数启动删除过程,我们将所有请求堆叠在一起而不等待,您可能希望等待每个请求,但即使您使浏览器崩溃,我认为它也会重复该过程比等待每个请求完成更快。
如果您打算使用此脚本,请在使用前对其进行测试。创建一个包含一些产品的 collection 并针对它创建 运行,以防出现问题。我已经在我这边测试过了,它可以工作,但这是我在午夜后 10 分钟内编写的代码,那里可能会有问题。
请记住,此脚本将删除您在 requestCollection(1231254125)
方法中指定的 collection 中的所有产品。
PS: 所有这些都可以使用私人应用程序以及产品范围设置为 read/write,使用 back-end 您选择的语言。主要区别在于您不需要 CSRF 令牌和我在上面设置的大部分 headers。但我喜欢不需要你拿出大手笔的快速解决方案。