woocommerce / wc-api-php

WooCommerce REST API PHP Library
https://packagist.org/packages/automattic/woocommerce
MIT License
528 stars 160 forks source link

[BUG] Uploading a product with this library fails and creates a broken product #340

Open Monniasza opened 1 year ago

Monniasza commented 1 year ago

Related to https://github.com/woocommerce/woocommerce/issues/40832

Describe the bug

When a new product is added via the Automattic\WooCommerce\Client library, the WordPress encounters a fatal error, and the library receives an invalid response:


Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: Error: <p>W witrynie wystąpił błąd krytyczny.</p><p><a href="https://wordpress.org/documentation/article/faq-troubleshooting/">Dowiedz się więcej o rozwiązywaniu problemów z WordPressem.</a></p> [internal_server_error] in C:\Users\oskar\vendor\automattic\woocommerce\src\WooCommerce\HttpClient\HttpClient.php:383 Stack trace: #0 C:\Users\oskar\vendor\automattic\woocommerce\src\WooCommerce\HttpClient\HttpClient.php(419): Automattic\WooCommerce\HttpClient\HttpClient->lookForErrors(Object(stdClass)) #1 C:\Users\oskar\vendor\automattic\woocommerce\src\WooCommerce\HttpClient\HttpClient.php(455): Automattic\WooCommerce\HttpClient\HttpClient->processResponse() #2 C:\Users\oskar\vendor\automattic\woocommerce\src\WooCommerce\Client.php(56): Automattic\WooCommerce\HttpClient\HttpClient->request('products', 'POST', Array) #3 C:\xampp\htdocs\konrad\wp-content\plugins\product-importer-plugin\insert-test.php(27): Automattic\WooCommerce\Client->post('products', Array) #4 {main} thrown in C:\Users\oskar\vendor\automattic\woocommerce\src\WooCommerce\HttpClient\HttpClient.php on line 383

The API credentials are correct, otherwise, an error would be different. The screenshot clearly shows that a product is broken: image image Below it is an example of a working product: image

Expected behavior

A product appears on the store page with the provided parameters

Actual behavior

The WooCommerce API returns an invalid response and causes a fatal error, and a broken product appears in the admin menu, but not the product listing. Additionally, the displayed product count is higher by 1 than actual count of working products

Steps to reproduce

Create a PHP script and visit it with the browser. Replace the autoload and API configs:

<?php
    //Include the WooCommerce API Client in Your PHP File
    require_once '\vendor\autoload.php'; // Replace with the path to your vendor/autoload.php
    // Include PHPExcel
    use PhpOffice\PhpSpreadsheet\IOFactory;
    use Automattic\WooCommerce\Client;
    // WooCommerce API credentials
    $api_url = 'http://example.com/'; // Your store's URL
    $api_key = 'ck_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Replace with your API key
    $api_secret = 'cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Replace with your API secret

    // Initialize the WooCommerce API client with your credentials
    $woocommerce = new Automattic\WooCommerce\Client($api_url, $api_key, $api_secret, ['version' => 'wc/v3']);

    // Define WooCommerce product data
    $product_data = [
        'name' => 'DebugTestProduct', // Product name without SKU
        'type' => 'simple',
        'regular_price' => '123',
        'description' => '',
        'short_description' => '',
        'stock_quantity' => 12,
        'sku' => 'PR123', // Product SKU (product number)
    ];

    // Add the product to WooCommerce
    $new_product = $woocommerce->post('products', $product_data);

    // Check for errors or handle success as needed
    if (is_wp_error($new_product)) {
        echo 'Error importing product: ' . $new_product->get_error_message() . '<br>';
    } else {
        echo 'Product imported successfully: ' . $new_product->name . '<br>';
    }
?>