2017-11-27 7 views
3

rest v2 APIエンドポイントを使用すると、属性/属性の用語をバリエーションにリンクする際に問題があります。 (https://woocommerce.github.io/woocommerce-rest-api-docs/?php#batch-update-product-variationsWooCommerce API期待通りに機能しない製品バリエーションを作成する

グローバル属性にリンクするバリエーションを作成するときに、APIから正しい応答を得るにはどうすればよいですか。 (コードの底部の下、そのAPIの呼び出しおよび応答を有する)以下

<?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>'; 

は上記$variations_responseの関連部分です。グローバル属性の場合、IDは0であってはなりません。実際の属性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" 
    } 
    } 

答えて

2

WooCommerce rest V2 APIを使用して属性の名前に "attribute"を使用することはできません。

これは一般的な問題ではありませんが、この例では_attribute付きのスラッグを生成するため、この例は機能しません。

関連する問題