Last active
July 11, 2017 13:33
-
-
Save doubleedesign/664264675d29ea6bea730bdf778caff9 to your computer and use it in GitHub Desktop.
Create WooCommerce Advanced Notifications recipient upon user registration, and add authors as notification recipients on the products they create automatically. First function sets the username as the notification recipient name, user's email as the notification email address (doesn't set any of the other fields, though these could be added), a…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Create a WooCommerce Advanced Notifications recipient when a user registers | |
add_action( 'user_register', 'doublee_also_add_a_wc_notification_recipient', 10, 1 ); | |
function doublee_also_add_a_wc_notification_recipient( $user_id ) { | |
global $wpdb; | |
if ( isset( $_POST['username'] ) ) { // triggered by my front-end form (Ninja Form). Your field name may be different | |
$doublee_recipient_name = sanitize_text_field( stripslashes( $_POST['username'] ) ); | |
} | |
if ( isset( $_POST['user_login'] ) ) { // triggered when a user is manually added in the dashboard | |
$doublee_recipient_name = sanitize_text_field( stripslashes( $_POST['user_login'] ) ); | |
} | |
if ( isset( $_POST['email'] ) ) { | |
$doublee_recipient_email = sanitize_text_field( stripslashes( $_POST['email'] ) ); | |
} | |
$result = $wpdb->insert( | |
"{$wpdb->prefix}advanced_notifications", | |
array( | |
'recipient_name' => $doublee_recipient_name, | |
'recipient_email' => $doublee_recipient_email, | |
) | |
); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full description:
These snippets create an WooCommerce Advanced Notifications recipient upon user registration, and add product authors as notification recipients on the products they create automatically.
The first function creates a notification recipient when a user is created. It sets the username as the notification recipient name, user's email as the notification email address (doesn't set any of the other fields, though these could be added), and adds Purchases as the notification type (as that's all I needed for my use case).
The second function gets the author of a product, uses their recipient name (which is the same as their username) to get their notification ID and assign that to the product's notifications upon save, making the author a notification recipient automatically.