woocommerce / woocommerce/wc-api-php

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

Open
#340 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stale
Dominant language
PHP
Stars
546
Forks
158
Avg merge
3h 21m
Merged PRs (30d)
2

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the POST to products using the provided PHP script, then trace src/WooCommerce/Client.php and src/WooCommerce/HttpClient/HttpClient.php around the reported lines 383, 419, and 455. Determine whether the client or the server response accounts for the fatal error; done means the product is created successfully without an invalid response or broken listing entry.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, wordpress
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.