How can I use $_SESSION?
You listed all the elements you need. You just need to swap out the set and get you were originally using with the ones you provided.

This:

$loop = $_SESSION['loop'];


Will become this:

$loop = Drupal::service('user.private_tempstore')-&gt;get('mymodule_name')-&gt;get('loop');


And this:

$_SESSION['loop'] = $loop + 1;


Will become this:

\Drupal::service('user.private_tempstore')-&gt;get('mymodule_name')-&gt;set('loop', $loop + 1);


For your other elements, you can do the following.

This:

$_SESSION['code'][$loop] = $code;


Becomes this:

\Drupal::service('user.private_tempstore')-&gt;get('mymodule_name')-&gt;set('code', [$loop =&gt; $code]);


And this:

$_SESSION['price'][$loop] = $price;


Becomes this:

\Drupal::service('user.private_tempstore')-&gt;get('mymodule_name')-&gt;set('price', [$loop =&gt; $price]);