Solved

Not adding Product options and variants to Shopify product

devdightinfotec
Shopify Partner
2 0 0

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?

Accepted Solution (1)

Liam
Shopify Staff
2731 302 783

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:

  1. 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.

  2. 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.

  3. 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

View solution in original post

Replies 2 (2)

Liam
Shopify Staff
2731 302 783

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:

  1. 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.

  2. 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.

  3. 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

devdightinfotec
Shopify Partner
2 0 0

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" ]
                ],
            );
        ];