woocommerce – How to validate custom fields before inserting

I am working on a woocommerce + dokan website with the help of advanced custom fields. In my store, I have to add some custom attributes to a product and then validate the custom attributes. I can only save the new product details if the custom attributes pass the validation.

However, I have a hard time learning what hook I should use to validate these custom attributes. And I'm having a hard time finding the right hook to print my error messages on the Dokan product pages. Here is a simple supplement that I wrote and that did not achieve my goals:

function johnproject_save_check_shipping ($ post_id) {

if (! is_valid_shipping_destination ($ _ POST["attribute_values"][1][0], $ _POST["attribute_values"][2][0]))
$ error = $ WP_Error ("bankrupt", "Your product does not qualify for shipping, check your specifications carefully and try again.");

false return

}
add_action (& # 39; save_post & # 39 ;, & # 39; johnproject_save_check_shipping & # 39;);

johnproject_presave_check_shipping ($ data) {function

if (! is_valid_shipping_destination ($ _ POST["attribute_values"][1][0], $ _POST["attribute_values"][2][0]))
$ error = $ WP_Error ("bankrupt", "Your product does not qualify for shipping, check your specifications carefully and try again.");


returns $ data;
}

add_filter (& # 39; wp_insert_post_data & # 39 ;, & # 39; johnproject_presave_check_shipping & # 39;);

function is_valid_shipping_destination ($ productType, $ productVariation)
{
// for testing purposes, always return false to see if errors are reported on the product page
false return;
}

When I press save on the page of my Dokan product, I still continue to create a product or update a product. The only way I can make the products not created or updated is if I put a die() statement in the is_valid_shipping_destination function.

Can someone tell me what is the correct way to stop a statement Create / Update or undo a database transaction from the PHP code in my add-on?