DD Tag to check if user purchased product

I’m not a developer, I’m an illustrator and designer, so forgive me if this is not well optimized.
I worked with chatGPT for several hours (again, I have no idea how to PHP) to create a dynamic data tag that will output “true” if the current user has purchased the current product, else “false”.
We went through several dozen variations until I finally got the following code.

I plan on using this to create recommendations and a dashboard that has a collector’s vibe.
I hope anyone else might find it useful.
If you see optimization opportunities, lemme know.

// Add the dynamic data tag “RSD Purchased” to the “RSD Tags” group.
add_filter( ‘bricks/dynamic_tags_list’, ‘add_rsd_purchased_tag’ );
function add_rsd_purchased_tag( $tags ) {
// Ensure your tag is unique (best to prefix it)
$tags[] = [
‘name’ => ‘{rsd_purchased}’,
‘label’ => ‘RSD Purchased’,
‘group’ => ‘RSD Tags’,
];

return $tags;
}

// Implement the custom logic to check if the user has purchased the current product.
add_filter( ‘bricks/dynamic_data/render_tag’, ‘get_rsd_purchased_value’, 10, 3 );
function get_rsd_purchased_value( $tag, $post, $context = ‘text’ ) {
// $tag is the tag name without the curly braces
// Only look for dynamic tag rsd_purchased
if ( $tag !== ‘rsd_purchased’ ) {
return $tag;
}

// Get the current user ID and product ID
$current_user_id = get_current_user_id();
$current_product_id = $post->ID;

// Check if the user has purchased the product with order status “processing” or “completed”
$has_purchased = false;

if ( wc_customer_bought_product( $current_user_id, $current_user_id, $current_product_id ) ) {
$has_purchased = true;
}

// Return “true” or “false” based on the purchase status
return $has_purchased ? ‘true’ : ‘false’;
}

// Replace the dynamic tag with the value when rendering the content.
add_filter( ‘bricks/dynamic_data/render_content’, ‘render_rsd_purchased_value’, 10, 3 );
add_filter( ‘bricks/frontend/render_data’, ‘render_rsd_purchased_value’, 10, 2 );
function render_rsd_purchased_value( $content, $post, $context = ‘text’ ) {
// $content might consist of HTML and other dynamic tags
// Only look for dynamic tag {rsd_purchased}
if ( strpos( $content, ‘{rsd_purchased}’ ) === false ) {
return $content;
}

// Do your custom logic here, you should define get_rsd_purchased_value() function
$my_value = get_rsd_purchased_value( ‘rsd_purchased’, $post, $context );

// Replace the tag with the value you want to display
$content = str_replace( ‘{rsd_purchased}’, $my_value, $content );

return $content;
}

One error I just noticed is that this fails when choosing to show or hide listings in the query