How to remove the SKU numbers from Ecwid in WordPress
If you’re wanting to remove the SKU numbers from Ecwid in WordPress – To remove starting with “p-” from every.
How to remove the SKU numbers from Ecwid in WordPress
Posted on
If you’re wanting to remove the SKU numbers from Ecwid in WordPress – To remove starting with “p-” from every Ecwid product, you can use the Ecwid API and JavaScript to fetch products, identify SKUs starting with “p-“, and then update them without this prefix.
Here’s a JavaScript code snippet for this:
/* Add to Custom JS */
// Ecwid Store settings
const storeId = ‘YOUR_STORE_ID’;
const token = ‘YOUR_ACCESS_TOKEN’;
// Function to fetch all products
async function fetchAllProducts() {
const response = await fetch(`https://app.ecwid.com/api/v3/${storeId}/products?token=${token}`);
const data = await response.json();
return data.items;
}
// Function to update product SKU
async function updateProductSKU(productId, newSku) {
await fetch(`https://app.ecwid.com/api/v3/${storeId}/products/${productId}?token=${token}`, {
method: ‘PUT’,
headers: { ‘Content-Type’: ‘application/json’ },
body: JSON.stringify({ sku: newSku })
});
}
// Main function to process all products
async function processProducts() {
const products = await fetchAllProducts();
for (const product of products) {
if (product.sku && product.sku.startsWith(‘p-‘)) {
const newSku = product.sku.replace(/^p-/, ”);
await updateProductSKU(product.id, newSku);
console.log(`Updated SKU for product ID ${product.id}: ${newSku}`);
}
}
}
// Run the main function
processProducts().then(() => console.log(“SKU update process completed.”)).catch(console.error);