Normalization using DENSE_RANK and LAST_INSERT_ID
You don't need to stand on your head to get the id:
Step 1: Populate Orders in a single statement:
INSERT INTO Orders (scanId, customerId)   -- Leave out; `id` it will be set
    SELECT DISTINCT scanId, customerId FROM temp;

Step 2: Populate the other table:
INSERT INTO OrderDetails (orderId, productId)
    SELECT Orders.id AS orderId,
           temp.productId
        FROM Orders
        JOIN temp USING (scanId, customerId);

It would be helpful to have
temp:  INDEX(scanId, customerId)