3
votes

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

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)

<?php
require_once 'wooapi/vendor/autoload.php';

use Automattic\WooCommerce\Client;

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

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

echo '$attributes_response';
echo '<pre>';
var_dump($attributes_response);
echo '</pre>';


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


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


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

echo '$products_response';
echo '<pre>';
var_dump($products_response);
echo '</pre>';


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

echo '$variations_response';
echo '<pre>';
var_dump($variations_response);
echo '</pre>';

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

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

1 Answers

2
votes

You can not use "attribute" in the name of the attribute using the WooCommerce rest V2 API.

This is not a common issue but this example will not work because it generates a slug with _attribute in it.