Well it’s more about the functionality of the woocommerce PUT function, since it seems to always overwrite images rather than add to the existing gallery. The “custom code” was just for context.
The problem is I don’t have the option of uploading all the images at once with POST, so how can I do it one at a time with PUT?