Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Monniasza opened this issue Oct 17, 2023 · 0 comments

Comments

@Monniasza
Copy link

Related to woocommerce/woocommerce#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>';
	}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant