How to set only variant or options for product variant through API #943
-
Is it possible to update only the options or the variants of a product variant through the PHP API? |
Beta Was this translation helpful? Give feedback.
Answered by
duncanmcclean
Nov 28, 2023
Replies: 1 comment 1 reply
-
Hey 👋 You can update both the options & variables like this, using the $product->productVariants([
'variants' => [
['name' => 'Colour', 'values' => ['Red', 'Green', 'Blue']],
['name' => 'Size', 'values' => ['Small', 'Medium']]
],
'options' => [
[
'key' => 'Red_Small',
'variant' => 'Red, Small',
'price' => 1599,
],
[
'key' => 'Red_Medium',
'variant' => 'Red, Medium',
'price' => 2950,
],
// Green_Small
// Green_Medium
// Blue_Small
// Blue_Medium
],
]); If you want to update only the variants or options while keeping the existing values, you still need to include both in your update. This is because the For instance, if you want to update the options but leave the variants unchanged, you can do something like this: $product->productVariants([
'variants' => $product->productVariants()['options'],
'options' => [
[
'key' => 'Red_Small',
'variant' => 'Red, Small',
'price' => 1599,
],
[
'key' => 'Red_Medium',
'variant' => 'Red, Medium',
'price' => 2950,
],
// Green_Small
// Green_Medium
// Blue_Small
// Blue_Medium
],
]); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
rolinbos
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey 👋
You can update both the options & variables like this, using the
productVariants
method:If you want to update only the variants or options while keeping the existing values, you still need to include both in your update. …