Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a custom table instead of user metas #22

Closed
mircobabini opened this issue Dec 14, 2020 · 6 comments · Fixed by #44
Closed

Use a custom table instead of user metas #22

mircobabini opened this issue Dec 14, 2020 · 6 comments · Fixed by #44

Comments

@mircobabini
Copy link
Member

Is your feature request related to a problem? Please describe.
There's no way to check who generated a gift. The pmprogl_gift_codes_purchased is serialized so no lightweight reverse lookup. This is not a good approach on high traffic websites with thousands of users/orders.

Describe the solution you'd like
I would implement a pmpro_gifts table with at list:
discount_id | giver_user_id | receiver_user_id (to be set when claimed)

@mircobabini
Copy link
Member Author

I prepared a lookup table implementation https://gist.github.com/mircobabini/c694770e217e483b249be9941eb07ff3

Todo: update lookup table's row when a code is claimed.

@dparker1005
Copy link
Member

In #18 , there is a proposal to save the gift code that is purchased during a checkout in order meta. If we go with that approach, the process to find the order with a given order meta and then find the purchaser for that order should not be too bad.

@mircobabini
Copy link
Member Author

mircobabini commented Jun 24, 2021

This is good. Since I have a production website with 500+ gifts generated, which heavily relies on this feature (implemented with the additional lookup table), I think I can work on this one and provide a PR.

I will change the current logic to use order metas (🎉 first implementation using order metas afaik!) so we already have a live website to make tests.

To be defined:

  • What should we do in case someone is using an old version of PMPro without order metas support? Probably just nothing.
  • How can we migrate the old datas from user metas to order metas? Action scheduler on plugin update?

@mircobabini
Copy link
Member Author

We would have 3 order metas:

  • giver user_id: added after order success, for gifts levels
  • gift_discount_id: added as soon as discount code is generated
  • receiver_user_id: added as soon as the discount code is consumed

@dparker1005
Copy link
Member

dparker1005 commented Jul 12, 2021

I think the only order meta that we really need is the gift_discount_id, probably will be named pmprogl_code_id.

Once that meta is in place, you can get the giver's user ID by finding the order associated with a gift and getting the user_id for the order, and you can get the receiver's user_id by querying the orders table for the order that used the gift code and getting the user_id for that order. I think having functions for those would be nice, though saving them into the order meta individually doesn't seem necessary.

What should we do in case someone is using an old version of PMPro without order metas support? Probably just nothing.

The order meta table has been out for around 9 months now, so hopefully there aren't too many sites without this functionality. I don't think it's worth building out new functionality only for people who haven't updated PMPro in the last 9 months since not associating gift codes with orders is how the plugin currently woks anyways. We should just check that the PMPro version > 2.5 before running the order meta code.

How can we migrate the old datas from user metas to order metas? Action scheduler on plugin update?

I don't think that there's a great way to tie old gift codes to their respective orders. In order to find the order that was likely used to purchase a gift code, we would need to:

  1. Get the start date of the gift code
  2. Get the user associated with the gift code by looking through usermeta
  3. Find an order for that user for the day after the gift code starts

This process can run into issues if multiple gift codes were purchased on the same day or if the start date of the code or the order date is changed. Perhaps a quicker solution would be to add a field to the "Edit Order" page for admins to manually link a gift code to the order, though this would not be sufficient for larger sites.

Happy to keep discussing if you have other ideas.

@dparker1005
Copy link
Member

#44 has the code necessary to store the gift code that is purchased into order meta, and I have created a new issue #45 to continue the migration process discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants