0

I run an ecommerce site on top of wordpress.

There is a particular meta field that users fill out when they checkout with me. Let's say it is the address of the user.

I would like to setup my site such that, no two users can checkout/create accounts with the same address.

Very similar to the functionality built into wordpress such that no two users can have the same email. The only difference if that it would be based on a custom meta field on the user instead.

Or alternatively, another approach would be to run a custom php hook on checkout that will query all users on the site and see if any contain a matching meta field. From here I can reject the order & send an email to the user notifying them of the issue so I can followup and try to resolve the problem with them. Is there a way to query all users for a matching meta field that doesn't place a heavy load on the server? I was thinking I could make a separate database entry just for this meta-field so I can query it quickly, but I feel that might be over-engineering the issue.

Either two approaches could work.

Thanks so much for the advice!

1 Answer 1

0

You can use the add_post_meta and update_post_meta filters to accomplish this. If you return anything other than null in these filters the post meta will not be saved.

add_filter('add_post_metadata', 'check_metadata', 10, 5);

function check_metadata($check, $object_id, $meta_key, $meta_value, $prev_value){

/*** Check if the metadata exists via sql ***/
    if($exists) {
         /*** Notify you or something ***/
         return true;
     else {
         return;
     }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.