5

I want to add variable product via woo commerce API. I successfully added the normal product via node js and php but need some example how to add variable product. I searched in documentation https://github.com/woothemes/woocommerce/tree/master/includes/api but couldn't find any sample source code to add variable product. Is there any sample source code available to add variable product via woo commerce API. Even here also there is no sample source code to add variable product.

I used the Api and My code is as follow

<?php

require_once( 'lib/woocommerce-api.php' );

$client = new WC_API_Client( 'http://example.com', 'ck_xxxxxxxxxxxxxxxxxxxxxxx8a4e', 'cs_xxxxxxxxxxxxxxxxxxxxxxxd701acf41c');
// echo "<pre>"; print_r($client->index->get());
// die;
$data = [
    'product' => [
        'title' => 'Preremium112 Quality',
        'type' => 'variable',
        'regular_price' => '21.99',
        'sku' => 'modrerts12344',
        'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
        'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
        'categories' => [
            9,
            14
        ],
        'product_attribute' => [
            'name' => 'case_model',

            'type' => 'select',
            'variation'=> 'true',
            'options' => [
                'Htc',
                'Samsung'
            ]
        ],

        'variations' => [

            ['regular_price' =>'12344',
                        'sku' => 'model_htc11234',
                        'attributes' => [
                            'name' => 'case_model',
                            'slug' => 'model',
                            'option'=> 'Htc'
                        ]],

            ['regular_price' =>'12344',
                        'sku' => 'model_samsung11234',
                        'attributes' => [
                            'name' => 'case_model',
                            'slug' => 'model',
                            'option'=> 'Samsung'
                        ]],
        ]

    ]
];

print_r($client->products->create($data));

The problem with above code is that product attributes are not updating

3
  • ohh really, TheDeadMedic, Howdy_McGee, kaiser the question whose answer you dont know, you mark them as off-topic. Please dont misuse your rights and power. Mar 28, 2016 at 19:58
  • If you dont know answer, keep quite and lets other answers Mar 28, 2016 at 19:59
  • "off-topic" as you marked is it really? I am askig question of woocommerce that is related to wordpress, is that off topic and not related to wordpress. Mar 28, 2016 at 20:00

0

Browse other questions tagged or ask your own question.