Sometimes my enemy ejects all cargo before exploding, sometimes they don't. How to guarantee I will be able to scoop their cargo before they explode?
There is only one way to ensure that you can get the commander's cargo before killing them, and that is to use the hatch breaker limpet.
Taken from the above link, the hatch breaker works as follows
[To use] make sure that the module is equipped and bound to a fire group. Take down the target's shields, maneuver close to the target's cargo hatch, shoot the Hatch Breaker and hope that it attached to the target's ship. If it is successful then cargo should spill out of the target's cargo hold.
So, rather than kill them, you need to reduce their shields and then steal their cargo.
Another option, if you are happy to not kill the target and are attacking a real player and not a NPC, is to send a comms to the commander telling them to dump their cargo, or you will blow them up. The advantage this has, is that you can ask them to jettison (abandon) the cargo, so that you don't have to sell the goods on the black market. This means higher profits, and the trader ship may be willing to do this in exchange for not being blown up (and having to pay the insurance costs).
There is another, but less reliable, method:
Target the ship's cargo hatch in the sub-systems menu (or by cycling through sub-systems with the appropriate configured controls) and aim for it (gimbals and turrets will automatically aim for any targeted sub-system).
When it is damaged enough, it will spill cargo. The cargo is still marked as stolen.