Managing product variants
The following guide illustrates product variant management flows using Bootic’s official Ruby API client.
1. Configure the client.
Read how to configure the Ruby client library to get the library ready.
2. Instantiate the client
The following creates a Bootic client instance for server-to-server scripts.
client = BooticClient.client(:client_credentials, scope: 'admin')
3. Load the root resource.
root = client.root
The root resource includes information on your current scope and the shops you own. From there you can navigate to other resources and take actions such as creating, updating or deleting products and their variants.
Use case: create a variant for a known product.
Assumming that you already have the following product data:
{
"id": 1234,
"slug": "iphone-5",
"price": 1200
}
Create variant
Let’s create a variant for that product. For that we use the create_variant
link present in your shop:
# Grab the first shop you own
shop = root.shops[0]
# Create a variant for product 'iphone-5'
if shop.can?(:create_variant)
variant = shop.create_variant(product_id: 1234,
title: 'Black case',
stock: 111,
sku: 'iph5-blk',
available_if_no_stock: false
)
# success?
if variant.errors
puts "There was an error! - #{variant.errors.first.messages}"
else
puts "Variant ##{variant.id} created!"
end
end
Documentation for create_variant.
Update variant
We can use the shop’s update_variant
link to update an existing variant. For example, here we update the stock
count for a variant of ID “3”.
if shop.can?(:update_variant)
variant = shop.update_variant(id: 3,
stock: 4
)
if variant.errors)
puts "There was an error! - #{variant.errors.first.messages}"
else
puts "Variant ##{variant.id} updated!"
end
end
Documentation for update_variant.
Delete variant
if shop.can?(:delete_variant)
shop.delete_variant(id: 3)
end
Documentation for delete_variant.