Step 1: Set Up Your Environment
- Install Dependencies: Ensure you have
dotenv
andshopify-api-node
installed:
npm install dotenv shopify-api-node
Create Your .env
File:
- Create a
.env
file in the root of your project with your Shopify store details:
SHOPIFY_STORE=your-store-name.myshopify.com
SHOPIFY_ACCESS_TOKEN=your-access-token
Step 2: Create Product Variants
Create a file, e.g., variants.js
, and add the following code to handle variant operations:
Create Variant
Step 2: Create Product Variants
Create a file, e.g., variants.js, and add the following code to handle variant operations:
Create Variant
Step 3: Retrieve Product Variants
Retrieve Variants
const retrieveVariants = async () => {
try {
const variants = await shopify.productVariant.list('product_id') // Replace 'product_id' with actual product ID
console.log('Variants retrieved:', variants)
} catch (error) {
console.error('Error retrieving variants:', error)
}
}
retrieveVariants()
Step 4: Update Product Variants
Update Variant
const updateVariant = async () => {
try {
const variant = await shopify.productVariant.update('variant_id', { // Replace 'variant_id' with actual variant ID
price: '29.99'
})
console.log('Variant updated:', variant)
} catch (error) {
console.error('Error updating variant:', error)
}
}
updateVariant()
Step 5: Delete Product Variants
Delete Variant
const deleteVariant = async () => {
try {
await shopify.productVariant.delete('variant_id') // Replace 'variant_id' with actual variant ID
console.log('Variant deleted')
} catch (error) {
console.error('Error deleting variant:', error)
}
}
deleteVariant()
Complete Example Code
Here’s a consolidated script to perform all CRUD operations on product variants:
require('dotenv').config()
const Shopify = require('shopify-api-node')
const shopify = new Shopify({
shopName: process.env.SHOPIFY_STORE,
accessToken: process.env.SHOPIFY_ACCESS_TOKEN
})
// Create Variant
const createVariant = async () => {
try {
const variant = await shopify.productVariant.create('product_id', { // Replace 'product_id' with actual product ID
option1: 'Default Title',
price: '19.99',
sku: 'SKU1234',
inventory_management: 'shopify',
inventory_quantity: 100,
})
console.log('Variant created:', variant)
} catch (error) {
console.error('Error creating variant:', error)
}
}
// Retrieve Variants
const retrieveVariants = async () => {
try {
const variants = await shopify.productVariant.list('product_id') // Replace 'product_id' with actual product ID
console.log('Variants retrieved:', variants)
} catch (error) {
console.error('Error retrieving variants:', error)
}
}
// Update Variant
const updateVariant = async () => {
try {
const variant = await shopify.productVariant.update('variant_id', { // Replace 'variant_id' with actual variant ID
price: '29.99'
})
console.log('Variant updated:', variant)
} catch (error) {
console.error('Error updating variant:', error)
}
}
// Delete Variant
const deleteVariant = async () => {
try {
await shopify.productVariant.delete('variant_id') // Replace 'variant_id' with actual variant ID
console.log('Variant deleted')
} catch (error) {
console.error('Error deleting variant:', error)
}
}
// Uncomment the function you want to execute
// createVariant()
// retrieveVariants()
// updateVariant()
// deleteVariant()
Running the Script
To run the script, uncomment the function you want to execute and run the following command in your terminal:
node variants.js
This setup should allow you to create, retrieve, update, and delete product variants in your Shopify store using the Shopify API and Node.js. If you have any further questions or need assistance, feel free to ask!
Leave a Reply