Step 1: Set Up Your Environment
Create a new Node.js project and install the required packages:
mkdir shopify-product-management
cd shopify-product-management
npm init -y
npm install shopify-api-node dotenv
Step 2: Create the .env
File
Create a .env
file in the root of your project to securely store your Shopify API credentials:
SHOPIFY_SHOP_NAME=your-shop-name
SHOPIFY_API_KEY=your-api-key
SHOPIFY_PASSWORD=your-password
Step 3: Create the Script
Create a file named manageProducts.js
and add the following code to update and delete products:
require('dotenv').config();
const Shopify = require('shopify-api-node');
const shopify = new Shopify({
shopName: process.env.SHOPIFY_SHOP_NAME,
apiKey: process.env.SHOPIFY_API_KEY,
password: process.env.SHOPIFY_PASSWORD
});
// Function to update a product
async function updateProduct(productId, updatedData) {
try {
const updatedProduct = await shopify.product.update(productId, updatedData);
console.log(`Product updated: ${updatedProduct.id}`);
} catch (error) {
console.error(`Failed to update product: ${error.message}`);
}
}
// Function to delete a product
async function deleteProduct(productId) {
try {
await shopify.product.delete(productId);
console.log(`Product deleted: ${productId}`);
} catch (error) {
console.error(`Failed to delete product: ${error.message}`);
}
}
// Example usage
const productIdToUpdate = 1234567890; // Replace with your product ID
const updatedData = {
title: 'Updated Product Title',
body_html: '<strong>Updated product description</strong>',
tags: 'updated, product, tags'
};
const productIdToDelete = 1234567890; // Replace with your product ID
updateProduct(productIdToUpdate, updatedData);
deleteProduct(productIdToDelete);
Explanation
- Environment Variables: The script uses environment variables to store sensitive information securely.
- Update Product Function: The
updateProduct
function updates a product’s details using the Shopify API’sproduct.update
method. - Delete Product Function: The
deleteProduct
function deletes a product using the Shopify API’sproduct.delete
method. - Example Usage: The script includes example usage of both functions, updating and deleting a product.
Step 4: Run the Script
Run your Node.js script to update and delete products:
node manageProducts.js
Summary
By following this guide, you will be able to update and delete products in your Shopify store using the Admin REST API with Node.js. Customize the script as needed to handle specific product attributes and additional requirements. This approach allows for efficient and automated product management in your Shopify store.
Leave a Reply