Problem with displaying error message in wordpress admin page

I created code that checks variants before adding them for duplicates. The PHP code itself works, but I have a problem with the JS which does not want to work with the PHP code and which checks for duplicates, which is why the error message does not want to be displayed. Can anyone help me what I’m doing wrong?

 public function __construct() {
        add_action('save_post', array($this, 'saveCustomVariantsMetabox'), 10, 2);
       
    }

public function saveCustomVariantsMetabox($post_id, $post) {

    // Verify the nonce before proceeding.
    if (!isset($_POST['custom_variants_nonce']) || !wp_verify_nonce($_POST['custom_variants_nonce'], plugin_basename(__FILE__))) {
        return $post_id;
    }

    // Check if the user has permission to edit the post.
    if (!current_user_can('edit_post', $post_id)) {
        return $post_id;
    }


    // Do not save during autosave or bulk edit.
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || isset($_REQUEST['bulk_edit'])) {
        return $post_id;
    }

    // Now, save the custom variants data
    $custom_variants = (isset($_POST['variants']) && !empty($_POST['variants'])) ? $_POST['variants'] : [];
 // Flag for duplicates
    $has_duplicates = false;
    // Sanitize and prepare for duplicate check
    $sanitized_variants = [];
    $existing_variants_hashes = []; // Store hashes of existing variants
    $has_duplicates = false; // Flag to track if duplicates are detected

    foreach ($custom_variants as $index => $variant) {
        // Sanitize user input.
        $variant['id'] = sanitize_text_field($variant['id']);
        $variant['dimensions'] = sanitize_text_field($variant['dimensions']);
        $variant['width'] = sanitize_text_field($variant['width']);
        $variant['height'] = sanitize_text_field($variant['height']);
        $variant['regular_price'] = floatval($variant['regular_price']);
        $variant['sale_price'] = floatval($variant['sale_price']);
        $variant['weight'] = sanitize_text_field($variant['weight']);
        $variant['sku'] = sanitize_text_field($variant['sku']);
        $variant['shipping_class'] = sanitize_text_field($variant['shipping_class']);
        
        $hash = md5($variant['width'] . $variant['height'] . $variant['regular_price']);

            if (in_array($hash, $existing_variants_hashes)) {
            // Set flag to true if duplicate is found
            $has_duplicates = true;
            break;
        }

        // Save hash for further comparison
        $existing_variants_hashes[] = $hash;
        $sanitized_variants[] = $variant;
    }
    
  // Check if duplicates were found
  if ($has_duplicates) {
        update_post_meta($post_id, 'custom_variants_duplicate', '1');
    } else {
        delete_post_meta($post_id, 'custom_variants_duplicate');
        update_post_meta($post_id, 'custom_variants', $sanitized_variants);
    }

    return $post_id;
}}
function check_duplicate_variants_before_publish() {
    global $post;

      // Pobierz wartość metadanych
    $hasDuplicateVariant = get_post_meta($post->ID, 'custom_variants_duplicate', true);

    // Konwertuj wartość na wartość logiczną
    $hasDuplicateVariant = $hasDuplicateVariant === '1' ? true : false;
    // Uruchom skrypt tylko jeśli istnieje duplikat
    if ($hasDuplicateVariant) {
        ?>
        <script type="text/javascript">
            jQuery(document).ready(function($) {
                $('#publish').click(function(e){
                    var hasDuplicateVariant = <?php echo $hasDuplicateVariant === '1' ? 'true' : 'false'; ?>;
                    if (hasDuplicateVariant) {
                        e.preventDefault();
                        alert('Wykryto duplikat wariantu. Proszę usunąć duplikat przed opublikowaniem posta.');
                        return false;
                    }
                    return true;
                });
            });
        </script>
        <?php
    }
}
add_action('admin_footer', 'check_duplicate_variants_before_publish');

I know it has something to do with the error on these lines, I know it’s something to do with an error with these lines, but I have no idea how to fix it

var hasDuplicateVariant = <?php echo $hasDuplicateVariant === '1' ? 'true' : 'false'; ?>;