Using /testfor in minecraft to check if a player has a certain item in their inventory

I am wondering how I can use the /testfor command in Minecraft 1.8+ to check whether a certain player (For example the nearest player to a command block) has a number of items.
For example, if a player pushes a button, this causes a command block to check if they have 10 iron ingots. If they do, the command block outputs a signal to another command block that takes 10 iron from them and gives them a diamond, making a simple shop.
IS this possible? If it is, could someone reply with the commands, as I am not very good with redstone. Thanks :)
Best Answer
If you do these setup commands:
/scoreboard objectives add items dummy
/stats entity @a set AffectedItems @p items
/scoreboard players set @a items 0
Then run this command: (N.B. The execute is ESSENTIAL)
/execute @a ~ ~ ~ /clear @p minecraft:iron_ingot 0
This will then mean that each player has the number of iron ingots in their inventory as their items score. This will allow you to process the event you need
Pictures about "Using /testfor in minecraft to check if a player has a certain item in their inventory"



Minecraft Bedrock - Test For Items In Inventory (Mobile/Xbox/PS4/Windows 10/Switch)
More answers regarding using /testfor in minecraft to check if a player has a certain item in their inventory
Answer 2
In 1.9/1.10: Make a repeating command block, needs redstone, that does:
/clear @p minecraft:(currency)
Replace "(currency)" with your currency in your example iron ingots. So do:
/clear @p minecraft:iron_ingot 0 1
Replace the 1 with however many you need to buy the second item, so 10. Now, place a conditional chain command block facing away from the repeating. Put:
/give @p minecraft:(bought item)
Replace bought item with the bought item, so diamond. Now however many, so 1. Then put 0. And you're done!
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Anna Shvets, Andrea Piacquadio, RF._.studio, Field Engineer