Import API: Variants
This API will import product variants to your shop. The API will generate new variant based on the attributes and attributes options passed in the data.
Please note that this is an all or none operation. It is currently not possible to partially generate variants based on supplied attributes and options, e.g. only generate Black x Large
BUT NOT Black x Small
.
If specifed parent product of a group of variants, or the attributes and attributes options specifed to generate variants currently don't exist in the shop, these will be created as well.
It is possible to generate variants for several different products with one function call.
Import Items Structure
$importItems
The array of $importItems
should be structured as shown below. This should be passed to $padloper->import()
as the first argument.
Please note the following special properties
that can be passed as specific options for generating each group of variants. These are part of the $importItems
array. Note that some of the options are required.
Name | Type | Description |
---|---|---|
product | string, int | Title or page ID of the product to generate a group of variants under. |
is_use_product_price (optional) | boolean | Whether to apply the main product price (if it already exists) to each of its generated variants. This setting is ignored if the option all_variants_price is set. |
all_variants_price (optional) | float | Optionally specify a price to be applied to all variants generated for given product. |
all_variants_enabled (optional) | boolean | Whether to enable all variants generated for given product. Non-enabled products and variants cannot be added to the. |
all_variants_allow_back_orders (optional) | boolean | Whether to allow overselling for all variants generated for given product. |
generate_variants | array | A multi-dimensional array of attributes and attribute options from which to generate variants for given product. Attributes and attribute options can be specified as page titles or IDs. |
$importItems = [
// variant: Oranges -> variant based on "Skin" ONLY
[
// @note: in this example, this product does not exist; we will create it
'product' => 'Oranges', // or ID as the product page ID
'all_variants_price' => 15.35,
'all_variants_enabled' => false, // default is true
// 'all_variants_allow_back_orders' => true, // default is false
'generate_variants' => [
// key (attribute) => value (attribute options)
// IDs in either key or value indicate the page exists - will be confirmed programmatically
// @note: this attribute and its attribute options does not exist; we will create it
'Skin' => ['Smooth', 'Rough', 'Polished'],
]
]
// MORE VARIANT DEFINITIONS ...
];
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$importType
The expected import type is variants
. This should be passed to $padloper->import()
as the second argument.
$importOptions
If required, the following import options can be passed to $padloper->import()
as the third argument when importing variants. This must be an array.
$importOptions = [
// @note: setting as unpublished!
'is_unpublished' => true
];
2
3
4
Example Import
The following example shows how to import several variant definitions for automatic generation of variants for your shop. Please inspect the resultant array for import notifications.
// IMPORT ITEMS
$importItems = [
// variant: Fruit Basket -> size x origin
[
'product' => 'Fruit Basket', // or 4589 as the page ID
'is_use_product_price' => false, // default is true unless 'all_variants_price' is set!
// 'all_variants_price' => 17.99,
'all_variants_enabled' => true, // default is true
'all_variants_allow_back_orders' => false, // default is false
'generate_variants' => [
// key (attribute) => value (attribute options)
// IDs in either key or value indicate the page exists - will be confirmed programmatically
'Origin' => ['Africa', 'South America', 'Europe', 'Asia'],
1803 => ['Small', 'Medium', 1806]
]
],
// variant: Fabric -> pattern x shape x material
[
// Fabric
'product' => 3375, // or title as the product page
// 'is_use_product_price' => true, // default is true unless 'all_variants_price' is set!
'all_variants_price' => 55.99,
// 'all_variants_enabled' => false, // default is true
// 'all_variants_allow_back_orders' => true, // default is false
'generate_variants' => [
// key (attribute) => value (attribute options)
// IDs in either key or value indicate the page exists - will be confirmed programmatically
'Origin' => ['Africa', 'South America', 'Europe'],
'Size' => ['Small', 'Medium', 'Large'],
'Material' => ['Cotton']
]
],
// variant: Oranges -> variant based on "Skin" ONLY
[
// @note: this product does not exist; we will create it
'product' => 'Oranges', // or ID as the product page ID
'all_variants_price' => 15.35,
'all_variants_enabled' => false, // default is true
// 'all_variants_allow_back_orders' => true, // default is false
'generate_variants' => [
// key (attribute) => value (attribute options)
// IDs in either key or value indicate the page exists - will be confirmed programmatically
// @note: this attribute and its attribute options does not exist; we will create it
'Skin' => ['Smooth', 'Rough', 'Polished'],
]
]
];
// IMPORT OPTIONS
// not passing any in this example
/** @var array $importResult */
$importResult = $padloper->import($importItems, 'variants');
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54