I have created some custom blocks in code that are shown on the Drupal Commerce checkout page. I would like the block to have differ content basing on which items are in the cart. How can I achieve this in code?
Around the time some Item1 (or Item2) is added to your cart, grant that user access to a role like "HasItem1InCart" (or "HasItem2InCart"). Use the Rules module to grant such access, using a Rules Action like "Add user role".
Within the block visibility settings for Block1 (which you want to show if Item1 is in the cart), only show that block to users with roles ... you probably guessed it .... HasItem1InCart (same for Block2 and HasItem2InCart).
Based on what I can gather from your question, You could use preprocess_page() hook on the checkout page and load the custom blocks there.
This way you can supply some kind of input to the block based on the page content(based on the items in the cart). Then you can change the block content based on the input given to the block.