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 variable $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).
query GetProductContent(
$productId: ID!
) {
customPost(by: { id: $productId }, customPostTypes: "product", status: any) {
content
@export(as: "content")
}
}
query RewriteProductContentWithChatGPT(
$openAIAPIKey: String!
$systemMessage: String! = "You are an English Content rewriter and a grammar checker"
$prompt: String! = "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: "
$model: String! = "gpt-4o-mini"
$temperature: Float
$maxCompletionTokens: Int
)
@depends(on: "GetProductContent")
{
promptWithContent: _strAppend(
after: $prompt
append: $content
)
openAIResponse: _sendJSONObjectItemHTTPRequest(input: {
url: "https://api.openai.com/v1/chat/completions",
method: POST,
options: {
auth: {
password: $openAIAPIKey
},
json: {
model: $model,
temperature: $temperature,
max_completion_tokens: $maxCompletionTokens,
messages: [
{
role: "system",
content: $systemMessage
},
{
role: "user",
content: $__promptWithContent
}
]
}
}
})
@underJSONObjectProperty(by: { key: "choices" })
@underArrayItem(index: 0)
@underJSONObjectProperty(by: { path: "message.content" })
@export(as: "rewrittenContent")
}
mutation UpdateProduct(
$productId: ID!
)
@depends(on: "RewriteProductContentWithChatGPT")
{
updateCustomPost(input: {
id: $productId,
customPostType: "product"
contentAs: {
html: $rewrittenContent
}
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPost {
__typename
...on CustomPost {
id
content
}
}
}
}
Automating the process
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
:
use GatoGraphQL\InternalGraphQLServer\GraphQLServer;
add_action(
'publish_product',
function (int $productId, WP_Post $post, string $oldStatus): void {
// Only execute when it's a newly-published product
if ($oldStatus === 'publish') {
return;
}
GraphQLServer::executePersistedQuery('improve-product-content-with-chatgpt', [
'productId' => $productId,
// Provide your Open AI's API Key
'openAIAPIKey' => '{ OPENAI_API_KEY }',
// Customize any of the other variables, for instance:
'maxCompletionTokens' => 5000,
]);
}, 10, 3
);