Queries LibraryAutomatically improving a new WooCommerce product's description with ChatGPT
Automatically improving a new WooCommerce product's description with ChatGPT
This query fetches the WooCommerce product with the provided ID, rewrites its content using ChatGPT, and stores it again.
(In the next section, we will automate the execution of this query, whenever the product is created.)
WooCommerce's product Custom Post Type must be made queryable via the GraphQL schema, as explained in guide Allowing access to Custom Post Types.
For that, go to the Settings page, click on the "Schema Elements Configuration > Custom Posts" tab, and select product from the list of queryable CPTs (if it's not already selected).
To connect to the OpenAI API, you must provide variables $openAIAPIKey with the API key.
You can optionally provide the system message and prompt to rewrite the post's content. If not provided, the following values are used:
System message ($systemMessage): "You are an English Content rewriter and a grammar checker"
Prompt ($prompt): "Please rewrite the following English text, by changing the simple A0-level words and sentences with more beautiful and elegant upper-level English words and sentences, while maintaining the original meaning: "
(The content string is appended at the end of the prompt.)
In addition, you can override the default value for variables $model ("gpt-4o-mini", check the list of OpenAI models) and provide values for $temperature and $maxCompletionTokens (both null by default).
We can use the Internal GraphQL Server to automatically execute the query whenever a new WooCommerce product is created.
To do this, first create a new persisted query with title "Improve Product Content With ChatGPT" (this will assign it slug improve-product-content-with-chatgpt), and the GraphQL query above.
Then, anywhere in your application (eg: in your functions.php file, a plugin, or a code snippet), add the following PHP code, which executes the query on hook publish_product: