Focusing on managing products, variants, and collections through the API.
I am getting issues with product options and variants
<?php
// Include the PHPShopifySDK
require_once('phpLibrary/vendor/autoload.php');
// Connect Shop to App
$config = array(
'ShopUrl' => 'dightinfotech.myshopify.com',
'AccessToken' => '********************************************',
);
// ShopifySDK object
PHPShopify\ShopifySDK::config($config);
$shopify = new PHPShopify\ShopifySDK;
$productData = [
'title' => 'Test Title',
'body_html' => 'This is demo descriptions',
'vendor' => 'Vendor',
'product_type' => 'Category',
'tags' => 'Blue',
'options' => array(
[
'name' => 'Size',
'values' => [ "Small", "Large" ]
],
[
'name' => 'Color',
'values' => [ "Red", "Blue" ]
],
);
];
$addProduct = $shopify->Product->post($productData);
$productId = $addProduct['id'];
$variantsData = array(
'variant' => array(
array(
'option1' => 'Small',
'otpion2' => 'Red',
'price' => 19.99,
'sku' => 'SKU123',
'available' => 200,
),
array(
'option1' => 'Small',
'option2' => 'Blue',
'price' => 19.99,
'sku' => 'SKU124',
'available' => 200,
),
array(
'option1' => 'Large',
'otpion2' => 'Red',
'price' => 21.99,
'sku' => 'SKU125',
'available' => 200
),
array(
'option1' => 'Large',
'option2' => 'Blue',
'price' => 21.99,
'sku' => 'SKU126',
'available' => 200,
)
)
);
$shopify->Product($productId)->Variant->post($variantsData);
Getting this error: Fatal error: Uncaught PHPShopify\Exception\ApiException: base - You need to add option values for Size
Also not adding the product options and variant correctly.
Can anyone help me to know what m missing?
Solved! Go to the solution
This is an accepted solution.
Hi Devdightinfotec,
The error message "You need to add option values for Size" suggests that the API is not recognizing the values for the 'Size' option in your product data.
Here are a few things you can check:
Check for Typographical Errors: In your variants data, you have a typographical error in the spelling of 'option2'. You've written 'otpion2' instead of 'option2'. This could be causing the problem.
Correct Structure for Variants Data: Make sure that the structure of your variants array is correct. It should be an array of arrays, where each sub-array represents a variant. The sub-array should include the correct option values that match the options defined in the product.
Update Product Options and Variants Together: When you're creating a new product with options and variants, you should include variants in the same product data array.
Try out the above steps to troubleshoot and let us know if you're still running into issues - hope this helps!
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
This is an accepted solution.
Hi Devdightinfotec,
The error message "You need to add option values for Size" suggests that the API is not recognizing the values for the 'Size' option in your product data.
Here are a few things you can check:
Check for Typographical Errors: In your variants data, you have a typographical error in the spelling of 'option2'. You've written 'otpion2' instead of 'option2'. This could be causing the problem.
Correct Structure for Variants Data: Make sure that the structure of your variants array is correct. It should be an array of arrays, where each sub-array represents a variant. The sub-array should include the correct option values that match the options defined in the product.
Update Product Options and Variants Together: When you're creating a new product with options and variants, you should include variants in the same product data array.
Try out the above steps to troubleshoot and let us know if you're still running into issues - hope this helps!
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Thank you So much @Liam . I fighured out and now code is working.
if anyone want to understand and check the solutions here it is
$productData = [
'title' => 'Test Title',
'body_html' => 'This is demo descriptions',
'vendor' => 'Vendor',
'product_type' => 'Category',
'tags' => 'Blue',
'variants' => array(
array(
'option1' => 'Small',
'option2' => 'Red',
'price' => 19.99,
'sku' => 'SKU123',
'available' => 200,
),
array(
'option1' => 'Small',
'option2' => 'Blue',
'price' => 19.99,
'sku' => 'SKU124',
'available' => 200,
),
array(
'option1' => 'Large',
'option2' => 'Red',
'price' => 21.99,
'sku' => 'SKU125',
'available' => 200
),
array(
'option1' => 'Large',
'option2' => 'Blue',
'price' => 21.99,
'sku' => 'SKU126',
'available' => 200,
)
),
'options' => array(
[
'name' => 'Size',
'values' => [ "Small", "Large" ]
],
[
'name' => 'Color',
'values' => [ "Red", "Blue" ]
],
);
];