Increase variable when buying from shop

Alright, so I'm trying to keep track of how much the player has bought from a shop in my game, y'know for stuff like "every 100 purchases you get a free potion!" but I'm not quite sure how to make it keep track of what the player's bought. (Aside from possibly brute forcing it by making a conditional branch check to see if the player has any more of items x, y, or z, after the shop menu in the event and if yes than increasing the variable) But I didn't know if there was a better way to accomplish what I'm planning.

As always any help is appreciated! 😀
First, you click on Control Variables button.
Second, operation: Set and Operand: Game Data ---> Item: Potion

However, I do not know to stop the checking loop, sorry for that.

Next, you can use conditional breach to check on items with the variable.

https://forums.rpgmakerweb.com/index.php?threads/vx-ace-need-a-variable-shop.90131/
It looks like an easy plugin to make. I might try it if you want.
With help of the Forums i could get this Scriptline which checks the current amount of all items (not the equiped ones, but thats not needed here)
You check how many Items before shop Visit Eventcommand, and you check again after the shop, than you compare the amount and safe it to a 3rd variable. than check if 3rd variable is 100 for example, than add a potion as gift.

The Scriptline checks the amount of items from id 1 to 100, if you have less than 100item ids this will result in game crash, so make sure to put the right number there.
Code:
$gameVariables.setValue(1, 0 )
for (var a = 1; a <= 100; a++) {  $gameVariables.setValue(1, $gameVariables.value(1) +
$gameParty.numItems($dataItems[a], false) ) }
Eventcommand Shop
Code:
$gameVariables.setValue(2, 0 )
for (var a = 1; a <= 100; a++) {  $gameVariables.setValue(2, $gameVariables.value(2) +
$gameParty.numItems($dataItems[a], false) ) }
Now Var2 sub Var1 = The difference in Items.
Var3 add Var2 (the amount of items bought)
if var3 equal or more 100? if yes add poition and set var3 to 0.
This should work in theory.

The Code Works, but the moment i finished it i realised a big Problem. What if the player not only Buys items but also Sells some, than
the number comparsion would not work anymore.
So you would maybe need a Plugin which changes how the shop works. Not sure.
Here's a typical eventing solution, though as Bex notes, if you can buy & sell at the shop then it'll see "2 bought and 1 sold" as +1. Depends whether you need that sort of distinction.
Spoiler
Code:
◆Comment:Number of potions now
◆Control Variables:#0001 potion count = The number of Potion
◆Comment:Enter shop
◆Shop Processing:Potion
◆Comment:Number of potions now - original number of potions
◆Control Variables:#0001 potion count *= -1
◆Control Variables:#0001 potion count += The number of Potion
◆If:potion count ≥ 5
  ◆Comment:Bought at least 5 potions!
  ◆Text:Actor1(0), Window, Bottom
  :    :Wow you really like potions, don't you?
  :    :Here's another one for being such a good
  :    :customer!
  ◆Change Items:Potion + 1
  ◆
:End
I have a notetag-based plugin for it here, but to give others a chance to contribute I'll wait 'til tomorrow to upload~ 🙏
@caethyril please, no need to tease us with waiting 😢. Iam sure your Plugin solves mostly all if not everything.
@Bex & @caethyril Well the shop was going to be purchase only anyway (It's sorta connected to the plot? The shopkeeper's... Unique) so at the very least we don't have to work around the player selling anything back to him
Okies here's my take on it: view/download file (Google Drive)
Spoiler: Embedded

It only adjusts variable values on buy/sell, so you'll still need some basic pre-/post-shop setup eventing for the "here's a bonus!" stuff.

@Bex Just trying to encourage others to step in if they want; like Poryg said, this is a fairly simple plugin request. I think I phrased it badly, though. 😴
@caethyril Thanks a bunch! I do have a question though, do you know whether or not this and Yanfly's "shop menu plugin" are compatible with each other? See I'm using that plugin because Yanfly's "more currency plugin" doesn't work without it, and I'm using that for the same shop, I would use this for and depending on how nice the plugins play with each other I might go ahead with brute force eventing it instead.
It looks like it should work if you load mine after Yanfly's Shop Menu Core; Yanfly still uses doBuy/doSell but overrides the default method. 🙂
Ok thanks, I figured it probably wouldn't be an issue but since I'm not javascript savvy (yet) I like to make sure beforehand

Original thread: https://forums.rpgmakerweb.com/threads/increase-variable-when-buying-from-shop.106661/