Hi,
You don't need any custom code for this, HikaShop has a "recently viewed" mode in its module.
- Go to Joomla, Extensions, Modules, New, and choose "Hikashop Module".
- In the module options set the content type to Products.
- Set the option "Recently viewed" to Yes.
- Set the number of products to display to 1, so only the very last viewed item is shown.
- Publish the module to the position where you want it.
The list of viewed products is kept in the visitor's session, most recent first, so with a limit of 1 the module shows the last product the visitor looked at. Because the session is preserved when the visitor logs in, a visitor who views Item 1, Item 2 then Item 3 and then logs in will indeed see Item 3 in the module.
One thing to keep in mind: this is based on the browsing session, not stored on the user account. It works within the same session (which covers your case of viewing products then logging in), but it does not persist across a different device or once the session has expired.