phpclassic / php-shopify

PHP SDK for Shopify API
Apache License 2.0
573 stars 210 forks source link

Not adding Product options and variants to Shopify product #311

Closed devdightinfotech closed 1 year ago

devdightinfotech commented 1 year ago

Using this ShopifySDK, I tried to create product options and variants, but it is not created. Here is my code:

$productData = [
            'title' => 'Product Test 1',
            'body_html' => 'This is product descriptions',
            'vendor' => 'Test Brand',
            'product_type' => 'Jeans',
            'tags' => 'blue', 
            'options' => array(
                array(
                    'name' => 'Color',
                   'values' => [
                        'Red',
                        'Blue'
                    ]
                ), 
               array(
                    'name' => 'Size',
                    'values' => [
                        'Small',
                        'Large'
                     ]
                ),

            )
        ];
        $addProduct = $shopify->Product->post($productData);
        $productId = $addProduct['id'];

       // Product options and variants
       $variantsData = array(
            'variants' => array(
                array(
                    'option1' => 'Small',
                    'option2' => 'Red', 
                    'price' => 19.99, 
                    'sku' => 'SKU123'
                ),
                array(
                    'option1' => 'Small',
                    'option2' => 'Blue',
                    'price' => 19.99,
                    'sku' => 'SKU124'
                ),
                array(
                    'option1' => 'Large',
                    'otpion2' => 'Red',
                    'price' => 21.99,
                    'sku' => 'SKU125'
                ),
                array(
                    'option1' => 'Large',
                    'option2' => 'Blue',
                    'price' => 21.99,
                    'sku' => 'SKU126'
                )

            ),
            )
        );
        $shopify->Product($productId)->Variant->post($variantsData);

Product is being created but not products options and variants

Help me to create products options and variants correctly in Shopify.

devdightinfotech commented 1 year ago

I got the solution here it is:

$productData = [
            'title' => 'Product Test 1',
            'body_html' => 'This is product descriptions',
            'vendor' => 'Test Brand',
            'product_type' => 'Jeans',
            'tags' => 'blue', 
           'variants' => array(
                array(
                    'option1' => 'Small',
                    'option2' => 'Red', 
                    'price' => 19.99, 
                    'sku' => 'SKU123'
                ),
                array(
                    'option1' => 'Small',
                    'option2' => 'Blue',
                    'price' => 19.99,
                    'sku' => 'SKU124'
                ),
                array(
                    'option1' => 'Large',
                    'option2' => 'Red',
                    'price' => 21.99,
                    'sku' => 'SKU125'
                ),
                array(
                    'option1' => 'Large',
                    'option2' => 'Blue',
                    'price' => 21.99,
                    'sku' => 'SKU126'
                )

            ),
            )
            'options' => array(

               array(
                    'name' => 'Size',
                    'values' => [
                        'Small',
                        'Large'
                     ]
                ),
                array(
                    'name' => 'Color',
                   'values' => [
                        'Red',
                        'Blue'
                    ]
                ), 
            )
        ];
        $addProduct = $shopify->Product->post($productData);