2016-10-29 6 views
0

woocommerce php APIの最新リポジトリをダウンロードしました。私は、テスト商品を作成するために、Woocommerce REST API Docsに従っています。ドキュメントのサンプルをコピーしましたが、動作していません。ここに私のコードです。製品を作成するための製品データが指定されていません[woocommerce_api_missing_product_data]

APIドキュメント:http://woocommerce.github.io/woocommerce-rest-api-docs/?php#create-a-product

<?php 

require_once("vendor/autoload.php"); 

use Automattic\WooCommerce\Client; 

$woocommerce = new Client(
    'http://wocom.dev', 
    'ck_c62182de9983593c9da6ed5c4642ff50e79e1b4f', 
    'cs_8f69a9d588abb9ca1b0309f22ac2cd7841496d56', 
    [ 
     'version' => 'v3', 
     'ssl_verify'=> 'false', 
     'timeout' => 30000 
    ] 
); 



$data = [ 
    'name' => 'Ship Your Idea', 
    'type' => 'variable', 
    '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' => [ 
     [ 
      'id' => 9 
     ], 
     [ 
      'id' => 14 
     ] 
    ], 
    'images' => [ 
     [ 
      'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg', 
      'position' => 0 
     ], 
     [ 
      'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_back.jpg', 
      'position' => 1 
     ], 
     [ 
      'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg', 
      'position' => 2 
     ], 
     [ 
      'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_back.jpg', 
      'position' => 3 
     ] 
    ], 
    'attributes' => [ 
     [ 
      'id' => 6, 
      'position' => 0, 
      'visible' => false, 
      'variation' => true, 
      'options' => [ 
       'Black', 
       'Green' 
      ] 
     ], 
     [ 
      'name' => 'Size', 
      'position' => 0, 
      'visible' => true, 
      'variation' => true, 
      'options' => [ 
       'S', 
       'M' 
      ] 
     ] 
    ], 
    'default_attributes' => [ 
     [ 
      'id' => 6, 
      'option' => 'Black' 
     ], 
     [ 
      'name' => 'Size', 
      'option' => 'S' 
     ] 
    ], 
    'variations' => [ 
     [ 
      'regular_price' => '19.99', 
      'image' => [ 
       [ 
        'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg', 
        'position' => 0 
       ] 
      ], 
      'attributes' => [ 
       [ 
        'id' => 6, 
        'option' => 'black' 
       ], 
       [ 
        'name' => 'Size', 
        'option' => 'S' 
       ] 
      ] 
     ], 
     [ 
      'regular_price' => '19.99', 
      'image' => [ 
       [ 
        'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg', 
        'position' => 0 
       ] 
      ], 
      'attributes' => [ 
       [ 
        'id' => 6, 
        'option' => 'green' 
       ], 
       [ 
        'name' => 'Size', 
        'option' => 'M' 
       ] 
      ] 
     ] 
    ] 
]; 




try 
{ 
    print_r($woocommerce->post('products', $data)); 

} 
catch(Exception $e) 
{ 
    echo $e->getMessage(); 
} 

エラー:

Error: No product data specified to create product [woocommerce_api_missing_product_data] 

答えて

0

代わりの$data = []使用$data['product'] = []

woocommerce APIは$data['product']が設定されているかどうかを確認するためにチェックされ、戻り[woocommerce_api_missing_product_data]空の場合。

woocommerce/includes/api/legacy/v2/class-wc-api-products.php

public function create_product($data) { 
$id = 0; 
    try { 
     if (! isset($data['product'])) { 
      throw new WC_API_Exception('woocommerce_api_missing_product_data', sprintf(__('No %1$s data specified to create %1$s', 'woocommerce'), 'product'), 400); 
     } 
+0

私はAPIのV2にリンクを参照してくださいが、v3は、ここで同じ文を含ん[woocommerce// API /レガシー/ V3 /クラス-WC-API-products.php含ま](HTTPSを: //github.com/woocommerce/woocommerce/blob/4dc579ff57c70577ea2e07376eb163db4c37dde3/includes/api/legacy/v3/class-wc-api-products.php#L254-L260) – rmddx

+0

ありがとうございます:)私にお試しください。 –

関連する問題