Filter and then sort products by availability
This is a "top n per group" problem but within the limits of MySQL.
You need to GROUP first then join back

SELECT 
   F.*
FROM
  (
  SELECT
    MIN(availability) AS MINavailability, id_product
  FROM
    foo
  GROUP BY
    id_product
  ) X
  JOIN
  foo F ON X.id_product = F.id_product AND X.MINavailability = F.availability
ORDER BY
    F.availability, F.id_product