You need to use the hooks WooCommerce provides. I would try it like following (untested):
add_action('woocommerce_single_product_summary', 'add_contact_form', 20); function add_contact_form() { global $product; if(!$product->is_in_stock( )) { echo do_shortcode('[name_of_shortcode]'); } }