Since you have a check for pAIs and carts I'd hazard a guess that you haven't been thorough enough with creating this code.

As mentioned, our guidelines do not allow copypasted code. This needs to be written to be "fit for purpose".