How to make hook in custom module to execute only on condition
Let the hook be called, but put your operations(that your hook executes) inside your required condition.

function mymodule_modulename_alter($parameters) {
  if ($condition) {
    // Your code.
  }  
}