WooCommerce API Creating Product Variations not working as expected

admin

Administrator
Staff member
I am having trouble linking attributes/ attribute terms to a variation when using the
Code:
/wp-json/wc/v2/products/<product_id>/variations/batch
rest v2 API endpoint. (<a href="https://woocommerce.github.io/woocommerce-rest-api-docs/?php#batch-update-product-variations" rel="nofollow noreferrer">https://woocommerce.github.io/woocommerce-rest-api-docs/?php#batch-update-product-variations</a>)

How can we get the correct response from the API when creating variations that link to global attributes. (Bottom of code below has that API call and response)

Code:
&lt;?php
require_once 'wooapi/vendor/autoload.php';

use Automattic\WooCommerce\Client;

$woocommerce = new Client(
    'http://localhost/wordpress/', 
    'ck_44b92c00ea35e6cc59c89c29051bf67c22e0df3a', 
    'cs_dd833592a1ef7a00a82c1711fd455db2e4c5bd15',
    [
        'wp_api' =&gt; true,
        'version' =&gt; 'wc/v2',
    ]
);

$attributes_response = $woocommerce-&gt;post('products/attributes/batch', 
array (
  'create' =&gt; 
  array (
    0 =&gt; 
    array (
      'name' =&gt; 'test attribute 1',
      'slug' =&gt; 'test_attribute_1',
    ),
    1 =&gt; 
    array (
      'name' =&gt; 'test attribute 2',
      'slug' =&gt; 'test_attribute_2',
    ),
  ),
));

echo '$attributes_response';
echo '&lt;pre&gt;';
var_dump($attributes_response);
echo '&lt;/pre&gt;';


$terms_response_1 = $woocommerce-&gt;post('products/attributes/'.$attributes_response['create'][0]['id'].'/terms/batch', 
array (
  'create' =&gt; 
  array (
    0 =&gt; 
    array (
      'name' =&gt; 'a',
    ),
    1 =&gt; 
    array (
      'name' =&gt; 'b',
    ),
    2 =&gt; 
    array (
      'name' =&gt; 'c',
    ),
  ),
));


$terms_response_2 = $woocommerce-&gt;post('products/attributes/'.$attributes_response['create'][1]['id'].'/terms/batch', 
array (
  'create' =&gt; 
  array (
    0 =&gt; 
    array (
      'name' =&gt; 'd',
    ),
    1 =&gt; 
    array (
      'name' =&gt; 'e',
    ),
    2 =&gt; 
    array (
      'name' =&gt; 'f',
    ),
  ),
));


$products_response = $woocommerce-&gt;post('products/batch', 
array (
  'create' =&gt; 
  array (
    0 =&gt; 
    array (
      'name' =&gt; 'Wilson Pro Overgrip 50 pack - Comfort - White',
      'regular_price' =&gt; '60.00',
      'description' =&gt; 'test',
      'stock_quantity' =&gt; 0,
      'manage_stock' =&gt; true,
      'sale_price' =&gt; '30.00',
      'date_on_sale_from' =&gt; '',
      'date_on_sale_to' =&gt; '',
      'images' =&gt; 
      array (
      ),
      'attributes' =&gt; 
      array (
        0 =&gt; 
        array (
          'id' =&gt; $attributes_response['create'][0]['id'],
          'name' =&gt; 'test attribute 1',
          'variation' =&gt; true,
          'visible' =&gt; true,
          'options' =&gt; 
          array (
            0 =&gt; 'b',
            1 =&gt; 'c',
          ),
        ),
        1 =&gt; 
        array (
          'id' =&gt; $attributes_response['create'][1]['id'],
          'name' =&gt; 'test attribute 2',
          'variation' =&gt; true,
          'visible' =&gt; true,
          'options' =&gt; 
          array (
            0 =&gt; 'e',
            1 =&gt; 'f',
          ),
        ),
      ),
      'type' =&gt; 'variable',
    ),
  ),
));

echo '$products_response';
echo '&lt;pre&gt;';
var_dump($products_response);
echo '&lt;/pre&gt;';


$variations_response = $woocommerce-&gt;post('products/'.$products_response['create'][0]['id'].'/variations/batch',
array (
  'create' =&gt; 
  array (
    0 =&gt; 
    array (
      'attributes' =&gt; 
      array (
        0 =&gt; 
        array (
          'id' =&gt; $attributes_response['create'][0]['id'],
          'option' =&gt; 'b',
        ),
        1 =&gt; 
        array (
          'id' =&gt; $attributes_response['create'][1]['id'],
          'option' =&gt; 'e',
        ),
      ),
      'manage_stock' =&gt; true,
      'regular_price' =&gt; '20.00',
    ),
    1 =&gt; 
    array (
      'attributes' =&gt; 
      array (
        0 =&gt; 
        array (
          'id' =&gt; $attributes_response['create'][0]['id'],
          'option' =&gt; 'c',
        ),
        1 =&gt; 
        array (
          'id' =&gt; $attributes_response['create'][1]['id'],
          'option' =&gt; 'f',
        ),
      ),
      'manage_stock' =&gt; true,
      'regular_price' =&gt; '40.00',
    ),
  ),
));

echo '$variations_response';
echo '&lt;pre&gt;';
var_dump($variations_response);
echo '&lt;/pre&gt;';

Below is the relevant portion of
Code:
$variations_response
above. For global attribute the ids should NOT be 0; but rather the actual attribute id.

Code:
["attributes"]=&gt;
  array(2) {
    [0]=&gt;
    array(3) {
      ["id"]=&gt;
      int(0)
      ["name"]=&gt;
      string(6) "test_1"
      ["option"]=&gt;
      string(1) "c"
    }
    [1]=&gt;
    array(3) {
      ["id"]=&gt;
      int(0)
      ["name"]=&gt;
      string(6) "test_2"
      ["option"]=&gt;
      string(1) "f"
    }
  }["attributes"]=&gt;
  array(2) {
    [0]=&gt;
    array(3) {
      ["id"]=&gt;
      int(0)
      ["name"]=&gt;
      string(6) "test_1"
      ["option"]=&gt;
      string(1) "c"
    }
    [1]=&gt;
    array(3) {
      ["id"]=&gt;
      int(0)
      ["name"]=&gt;
      string(6) "test_2"
      ["option"]=&gt;
      string(1) "f"
    }
  }