How to enable select2 on shipment add/update inside WordPress Admin page

Code goes to your active theme’s functions.php file

add_action( 'admin_enqueue_scripts', function( $hook ) {
    // Optional: load only on specific admin pages
    // if ( $hook !== 'post.php' && $hook !== 'edit.php' ) return;
    $post = $_GET['post'] ?? '';
    $action = $_GET['action'] ?? '';
    $post_type = $_GET['post_type'] ?? '';

    if(($post && $action === 'edit') || $post_type === 'wpcargo_shipment') {
        // Enqueue Select2 (WordPress already bundles it)
        wp_enqueue_style( 'wpcargo-select2-css', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css' );
        wp_enqueue_script( 'wpcargo-select2-js', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js', [ 'jquery' ], null, true );
    }
});

add_action('admin_footer', function(){
    $post = $_GET['post'] ?? '';
    $action = $_GET['action'] ?? '';
    $post_type = $_GET['post_type'] ?? '';

    if(($post && $action === 'edit') || $post_type === 'wpcargo_shipment') {
        ?>
            <script>
            jQuery(document).ready(function($){
                function initSelect2(){
                    let box = $('#poststuff');
                    if (box.length) {
                        box.find('select:not(.wpc_addressbook_autofill)').select2();
                    } else {
                        setTimeout(initSelect2, 200); // retry until metabox exists
                    }
                }
                initSelect2();
           });
           </script>
        <?php
    }
});