How to work with product attributes, options, and variants
1. How to work with product attributes
An attribute is a parameter or a characteristic of a product, like size or color. The administrator of the store sets attribute value.
For methods product.info and product.list, additional attributes are returned the same way as general - as fields of structure product.
In the response of API v1.1 additional fields are returned in additional_fields array.
Custom attributes can be divided into custom attributes of the product (are created and used for one specific product) or system custom attributes (are created on the store level and can be applied to all or some products).
For example, TV can have custom attributes like display size, screen resolution, etc.
API requests for working with attributes
Methods: product.list; product info
Fields: product, product.additional_fields, product.custom_fields
Use methods and fields above for getting names and values of all attributes of a product of any type.
Use product.attribute.list to get not only attribute value, but also additional attribute parameters, like
Use product.attribute.value.set to set value of particular attribute for the specific product. If the product doesn’t have such an attribute, it will be created.
How to add simple products with global and custom attributes
Let's say you need to add product with attribute value “Color” = “Green” and “Pattern” = “Stripes”
For this, make the following requests:
And then make 1 product.attribute.value.set request for each attribute, if needed.
For updating existing attributes it is best to use product.attribute.value.set method. If a product doesn’t have the attribute yet, then it will be created, but there are some limitations on some platforms.
How to get info on product attributes
All attributes of the product, both custom and global, can be accessed via product.list/product.info.
To make sure you get all product attributes, join arrays “additional_fields” and “custom_fields” from the response, because on different platforms attributes are stored in either first or the second array.
2. How to work with product options, option values
Options are product attributes that can be changed (selected) by store clients to choose specific product variant.
For example, option “Size” may have option values, like “Small”, “Medium”, etc.
The combination is the combination of all option values of a product (if it has a few value options).
API requests for working with options
Options are returned with all available values (product_options.option_items).
On some platforms, like WooCommerce, Magento, Opencart, etc. there are price modifiers that allow setting how the end price depends on option_value selection (price modifier, the field product_options.option_items.price).
The following methods work for Magento and Opencart:
3. How to work with configurable (variable) products, product variants
Configurable products (WooCommerce calls them “Variable”, Prestashop - "Product with combinations") let sellers offer a set of variations on a product, with control over prices, stock, image and more for each variation.
The shopper chooses the combination of options, and based on this the store suggests him some product variant, like “T-Shirt, green, medium size”. Store owner can set price, stock, image and other attributes for each variant.
On some platforms, like WooCommerce seller should always specify all possible attributes when editing the variant. However, if there is a possibility not to specify some attribute values, we exclude this attribute from combinations and variants.
There is a special type of options - custom options. When shopper chooses these custom options values, the product variant doesn’t change. For example, “T-Shirt, green, medium size” may have custom option “with/without printing”, but the choice of this option doesn’t change the product variant, it will stay the same in the shopping cart or order. Custom options are accessed the same as ordinary ones.
Methods to work with variants:
To form a variant, in those methods in the attribute parameter, you need to specify option values that are part of a combination and price modifier for each value.
On some platforms, like Shopify and Magento 2, price modifiers don’t count. Instead, price of the variant should be specified in the price parameter.
Example of request with price modifier
Example of request without price modifier
Method variant.add will also create options and option values specified in the attributes field (if the product was “simple” before (didn’t have options).
Methods to get info on variants:
Deprecated methods for working with variants:
How to add configurable product T-Shirt with 4 variants
Red T-Shirt size M
Green T-Shirt size M
Red T-Shirt size S
Green T-Shirt size S
Step 1: Add parent product
Step 2: Add parent product variants
How to get some order product info?
(e.g. the real-time product quantity in the store)
Step 1: Pay attention to the variant_id field in the response structure of order.info or order.list methods.
If the field is empty, then the order contains a simple product. If it is filled, then it means that some variant of configurable product is ordered.
Response example (part)
Step 2: If the field is empty, then you should use product.info method and specify product_id from the response above.
Step 3: If the field is not empty, then you should save from the response above field values product_id and variant_id.
Get info on product variants with saved product_id and specify product_id parameter.
Step 3.1: From children field choose variant with “id” that is identical to “variant_id” from the previous response.
Child_item.list and product.info methods can show an error message that the product is not found. It means that at the current time the product can not exist, but it was when the order was placed.