Woocommerce api仅显示产品ID

时间:2018-05-02 08:44:03

标签: php wordpress api woocommerce

大家好,这里只是一个简单的问题,我通过api成功展示了woocommerce的产品,下面是代码

<?php

include_once 'wc-api-php-master/vendor/autoload.php';

use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;

$woocommerce = new Client(
  '', 
  '', 
  '',
  [
      'wp_api' => true,
      'version' => 'wc/v2',
      'query_string_auth' => true // Force Basic Authentication as query string true and using under HTTPS
  ]
);

try {
    // Array of response results.
    $query = new WC_Product_Query( array(
        'return' => 'ids',
    ) );
    $products = $query->get_products();
    print_r($products);

    // Last request data.
    $lastRequest = $woocommerce->http->getRequest();
    $lastRequest->getUrl(); // Requested URL (string).
    $lastRequest->getMethod(); // Request method (string).
    $lastRequest->getParameters(); // Request parameters (array).
    $lastRequest->getHeaders(); // Request headers (array).
    $lastRequest->getBody(); // Request body (JSON).

    // Last response data.
    $lastResponse = $woocommerce->http->getResponse();
    $lastResponse->getCode(); // Response code (int).
    $lastResponse->getHeaders(); // Response headers (array).
    $lastResponse->getBody(); // Response body (JSON).

} catch (HttpClientException $e) {
    $e->getMessage(); // Error message.
    $e->getRequest(); // Last request data.
    $e->getResponse(); // Last response data.
}

?>

但它会显示所有详细信息,包括产品ID,产品名称,slug等,我如何才能显示产品ID?

感谢您的帮助!

1 个答案:

答案 0 :(得分:-1)

使用以下代码,您只会收到产品ID

$query = new WC_Product_Query( array(
    'return' => 'ids',
) );
$products = $query->get_products();
print_r($products);

它只会获得所有产品的ID。