Skip to content

Instantly share code, notes, and snippets.

@kadimi
Last active October 14, 2017 09:31
Show Gist options
  • Save kadimi/e6e5929b4b1ba3c1d17893d3e0638c63 to your computer and use it in GitHub Desktop.
Save kadimi/e6e5929b4b1ba3c1d17893d3e0638c63 to your computer and use it in GitHub Desktop.
Codeable #64026 - compatibble with WooCommerce 2.6.4 and 3.x
<?php
/**
* The message sender ID (0 for current user).
*/
define('CA64026_SENDER_ID', 1);
/**
* What username to use for guests.
*/
define('CA64026_NO_USERNAME', 'no_username');
/**
* The categories for which to send messages.
*/
define('CA64026_CATEGORIES', serialize ( [ 123, 321 ] ) );
/**
* Trigger sending messages when an order is completed.
*/
add_action( 'woocommerce_order_status_completed', 'ca64026_order_completed_cb' );
/**
* Sends message for products author in order.
*
* @param Integer $order_id The order id.
*/
function ca64026_order_completed_cb( $order_id ) {
foreach ( ca64026_get_order_products( $order_id ) as $order_product ) {
ca64026_send_message_for_order_product( $order_id, $order_product[ 'product_id' ]);
}
}
/**
* Get order items by order ID.
*
* @param Integer $order_id The order ID.
* @return Array The order items.
*/
function ca64026_get_order_products( $order_id ) {
$order = wc_get_order( $order_id );
return $order ? $order->get_items() : [];
}
/**
* Get product author.
*
* @param Integer $product_id The product ID.
* @return Integer The product author ID.
* @return WP_User|bool The product autho user object, false on failure.
*/
function ca64026_get_product_author( $product_id ) {
$product = get_post( $product_id );
return $product ? get_userdata( $product->post_author ) : false;
}
/**
* Get the user login by id.
*
* @param Integer $id The user ID.
* @return String|Bool The user name or false on error.
*/
function ca64026_get_user_login_by_id( $id ) {
$user = get_user_by( 'id', $id );
return $user ? $user->get('user_login') : false;
}
/**
* Send a customized message with BuddyPress.
*
* The message is send to the product author.
*
* @param Integer $order_id The parent order.
* @param Integer $product_id The product ID.
* @return int|bool|WP_Error ID of the message thread on success, false on failure.
*/
function ca64026_send_message_for_order_product( $order_id, $product_id ) {
$order = wc_get_order( $order_id );
$product = wc_get_product( $product_id);
/**
* Skip product if it's not in one of the categories in CA64026_CATEGORIES.
*/
$product_categories = wp_get_post_terms($product_id, 'product_cat', ["fields" => "ids"]);
$skip = ! array_intersect( unserialize( CA64026_CATEGORIES ), $product_categories );
if ( $skip ) {
return;
}
/**
* Get the buyer username, guests will have the username defined in the constant CA64026_NO_USERNAME.
*/
$buyer_username = ca64026_get_user_login_by_id( $order->get_user_id() );
if ( ! $buyer_username ) {
$buyer_username = CA64026_NO_USERNAME;
}
// We need <br /> tags, and then we need to replace them with \n, as expected by BuddyPress.
$billing_address = str_replace('<br />', "\n", wp_kses( $order->get_formatted_billing_address(), [ 'br' => [] ] ) );
// I'm using this weird concatenation to both the message and indentation in my IDE correct.
$template = ''
. 'Congrats, you have received an order from "%1$s", order #%2$d.' . "\n"
. "\n"
. 'The billing address is:' . "\n"
. '%3$s' . "\n"
. "\n"
. 'The product purchased was "%4$s".' . "\n"
;
$content = sprintf( $template, $buyer_username, $order_id, $billing_address, get_the_title( $product_id ) );
$message_args = [
'sender_id' => ( defined( 'CA64026_SENDER_ID' ) && CA64026_SENDER_ID )
? CA64026_SENDER_ID
: bp_loggedin_user_id()
,
'recipients' => ca64026_get_product_author( $product_id )->get('ID'),
'subject' => sprintf( __('New Order #%s', 'ca64026'), $order_id) ,
'content' => nl2br( $content ),
];
return messages_new_message( $message_args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment