我正在尝试使用Rest API创建一个woo商务产品提供我有一个消费者密钥:和消费者秘密:使用读取和写入身份验证我不知道哪里出错了PHP没有填充任何错误消息也没有产品是创建
<?php
require_once 'class-wc-api-client.php';
function addProduct()
{
$options = array(
'debug' => false,
'return_as_array' => false,
'validate_url' => false,
'timeout' => 30,
'ssl_verify' => false,
);
try {
$client = new WC_API_Client( 'http://example.com/demo/workpressistalledlocation/','xxxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxx',$options );
$client->products->create( array( 'sku' => '100001', 'title' => 'Superduper product', 'type' => 'simple', 'regular_price' => '21.50', 'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.', 'short_description' => 'Short description for product'));
print_r( $completed_orders );
} catch ( WC_API_Client_Exception $e ) {
echo $e->getMessage() . PHP_EOL;
echo $e->getCode() . PHP_EOL;
if ( $e instanceof WC_API_Client_HTTP_Exception ) {
print_r( $e->get_request() );
print_r( $e->get_response() );
}
}
}
答案 0 :(得分:1)
如果您还没有找到答案,可以试试这个。
仅在您使用woo-commerce rest api v2 / v3版本时使用
$product = array(
'title' => 'title_of_product',
'sku' => 'unique_sku',
'regular_price' => 'price',
'type' => 'simple/variable/grouped/external',
'description' => $this->tftimport_description,
'managing_stock' => $manage_wc_stock,
'status' => 'draft',
'enable_html_description' => true,
);
$wc_product = $this->get_wc_api_client()->products->create($product);
你还可以从下面的woocommerce官方api文档中查看有关rest api的更多帮助:
https://woothemes.github.io/woocommerce-rest-api-docs/?php#create-a-product