Description
Given a product with variants set up (with existing option groups),
trying to add a new option group to the product by calling POST /api/v1/products/:product_id/variants
with the variants
field but without the options
field
result in an error.
Expected Behavior
For a product that has existing option groups i.e. option_data
is not empty e.g.
{
"color": {
"label": {
"en": "Color"
},
"options": {
"black": {
"values": {
"en": "black"
},
"position": 1
},
"white": {
"values": {
"en": "white"
},
"position": 2
}
},
"position": 1
}
}
attempt to add another entry to the option_data
by calling POST /api/v1/products/:product_id/variants
with a request body with a variants
field but without an options
field e.g.
{
"variants": [
{
"label": "white s",
"price": 1,
"sku": "white-sss",
"options": {
"color": {
"en": "white"
},
"size": {
"en": "s"
}
},
"inventory": 1
},
{
"label": "black s",
"price": "1.00",
"sku": "black-sss",
"options": {
"color": {
"en": "black"
},
"size": {
"en": "s"
}
},
"inventory": 1
},
{
"label": "white m",
"price": 1,
"sku": "white-mmm",
"options": {
"color": {
"en": "white"
},
"size": {
"en": "m"
}
},
"inventory": 1
},
{
"label": "black m",
"price": "1.00",
"sku": "black-mmm",
"options": {
"color": {
"en": "black"
},
"size": {
"en": "m"
}
},
"inventory": 1
}
]
}
updates the option_data
column in DB to
{
"size": {
"label": {
"en": "S"
},
"options": {
"m": {
"values": {
"en": "m"
},
"position": 2
},
"s": {
"values": {
"en": "s"
},
"position": 1
}
},
"position": 2
},
"color": {
"label": {
"en": "Color"
},
"options": {
"black": {
"values": {
"en": "black"
},
"position": 1
},
"white": {
"values": {
"en": "white"
},
"position": 2
}
},
"position": 1
}
}
and the endpoint responds with success status.
Current Behavior
For a product that has existing option groups i.e. option_data
is not empty e.g.
{
"color": {
"label": {
"en": "Color"
},
"options": {
"black": {
"values": {
"en": "black"
},
"position": 1
},
"white": {
"values": {
"en": "white"
},
"position": 2
}
},
"position": 1
}
}
attempt to add another entry to the option_data
by calling POST /api/v1/products/:product_id/variants
with a request body with a variants
field but without an options
field e.g.
{
"variants": [
{
"label": "white s",
"price": 1,
"sku": "white-sss",
"options": {
"color": {
"en": "white"
},
"size": {
"en": "s"
}
},
"inventory": 1
},
{
"label": "black s",
"price": "1.00",
"sku": "black-sss",
"options": {
"color": {
"en": "black"
},
"size": {
"en": "s"
}
},
"inventory": 1
},
{
"label": "white m",
"price": 1,
"sku": "white-mmm",
"options": {
"color": {
"en": "white"
},
"size": {
"en": "m"
}
},
"inventory": 1
},
{
"label": "black m",
"price": "1.00",
"sku": "black-mmm",
"options": {
"color": {
"en": "black"
},
"size": {
"en": "m"
}
},
"inventory": 1
}
]
}
updates the option_data
column in DB to
{
"s": {
"label": {
"en": "S"
},
"options": {
"m": {
"values": {
"en": "m"
},
"position": 2
},
"s": {
"values": {
"en": "s"
},
"position": 1
}
},
"position": 2
},
"color": {
"label": {
"en": "Color"
},
"options": {
"black": {
"values": {
"en": "black"
},
"position": 1
},
"white": {
"values": {
"en": "white"
},
"position": 2
}
},
"position": 1
}
}
and the endpoint responds with an error Undefined index: size
with stack trace pointing to vendor/getcandy/candy-api/src/Core/Products/Models/ProductVariant.php:103
.
Possible Solution
Steps to Reproduce
- Create a product
- Create a variant for the product:
POST /api/v1/products/:product_id/variants
with request body:
{
"color": {
"label": {
"en": "Color"
},
"options": {
"black": {
"values": {
"en": "black"
},
"position": 1
},
"white": {
"values": {
"en": "white"
},
"position": 2
}
},
"position": 1
},
"variants": [
{
"label": "white",
"price": 1,
"sku": "white",
"options": {
"color": {
"en": "white"
}
},
"inventory": 1
},
{
"label": "black",
"price": "1.00",
"sku": "black",
"options": {
"color": {
"en": "black"
}
},
"inventory": 1
}
]
}
- Attempt to add a new option group by calling
POST /api/v1/products/:product_id/variants
with request body:
{
"variants": [
{
"label": "white s",
"price": 1,
"sku": "white-sss",
"options": {
"color": {
"en": "white"
},
"size": {
"en": "s"
}
},
"inventory": 1
},
{
"label": "black s",
"price": "1.00",
"sku": "black-sss",
"options": {
"color": {
"en": "black"
},
"size": {
"en": "s"
}
},
"inventory": 1
},
{
"label": "white m",
"price": 1,
"sku": "white-mmm",
"options": {
"color": {
"en": "white"
},
"size": {
"en": "m"
}
},
"inventory": 1
},
{
"label": "black m",
"price": "1.00",
"sku": "black-mmm",
"options": {
"color": {
"en": "black"
},
"size": {
"en": "m"
}
},
"inventory": 1
}
]
}
- Observe the API endpoint responds with an error
Undefined index: size
.
Context (Environment)
getcandy/candy-api: version 0.11.5
OS: MacOS 11.1
I want to add a new option group to a product that already has existing option groups.
For example, an admin could decide to create a new option group size
for a T-shirt product that already has a color
option group.
On the current admin hub, I find it impossible to achieve such a feat - it's only possible to add new options to an existing option group. Using the same example above, this means an admin could only add more options to the existing color
option group e.g. add grey
color to the options, but not adding a new option group size
.
Activity