Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doubleedesign/664264675d29ea6bea730bdf778caff9 to your computer and use it in GitHub Desktop.
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…
<?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,
)
);
}
?>
<?php
// When a product is saved, add its author as a notification recipient
add_action( 'added_post_meta', 'doublee_on_product_save', 10, 4 );
add_action( 'updated_post_meta', 'doublee_on_product_save', 10, 4 );
function doublee_on_product_save( $meta_id, $post_id, $meta_key, $meta_value ) {
if ( $meta_key == '_edit_lock' ) { // we've been editing the post
if ( get_post_type( $post_id ) == 'product' ) {
global $post;
global $wpdb;
// Get the author's username
$author_id = $post->post_author;
$username = get_the_author_meta('user_login', $author_id);
// Look for the author's username in the Advanced Notifications table and get their notification ID
$query = $wpdb->prepare( "SELECT `notification_id` FROM `wp_advanced_notifications` WHERE `recipient_name` = '$username'");
$notification_recipient_rows = $wpdb->get_results($query, OBJECT);
foreach ($notification_recipient_rows as $recipient) {
$recipient_id = $recipient->notification_id;
break; // there should only be one result, but this ensures we only get the first one
}
// Add the notification ID to this product by ID in the Advanced Notification Triggers table
$wpdb->query("INSERT INTO `wp_advanced_notification_triggers`(`notification_id`, `object_id`, `object_type`) VALUES ('$recipient_id', '$post_id' , 'product')");
}
}
}
?>
@doubleedesign
Copy link
Author

doubleedesign commented Jul 11, 2017

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.

  1. 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).

  2. 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.

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