Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Garconis/54b10149b576529fb8ac211b0c9da071 to your computer and use it in GitHub Desktop.
Save Garconis/54b10149b576529fb8ac211b0c9da071 to your computer and use it in GitHub Desktop.
WooCommerce | Add a message to the Order Complete email based on if a product is in certain categories
<?php
add_action( 'woocommerce_email_before_order_table', 'add_content', 20, 4 );
function add_content($order, $sent_to_admin, $plain_text, $email) {
// Adds to "completed order" email only
if ( $email->id == 'customer_completed_order' ) {
// set initial variables to false
$both = false;
$state = false;
$associate = false;
// look through each item in the order
foreach ( $order->get_items() as $item ){
// get the categories for each item
$categories = get_the_terms( $item['product_id'] , 'product_cat' );
// look through the categories
foreach( $categories as $category ) {
// if an item has this category, set this variable to true
if ($category->slug == 'state') {
$state = true;
}
// and if an item has this category, set this variable to true
if( $category->slug == 'associate' ) {
$associate = true;
}
}
}
// if the order contained item(s) with both State and Associate categories
if ($state && $associate) {
$variable_text = 'organization';
$variable_text_short = 'organization';
$variable_text_mailto = 'organization';
}
// if the order contained item(s) with State category
elseif ($state) {
$variable_text = 'state agency';
$variable_text_short = 'agency';
$variable_text_mailto = 'state%20agency';
}
// if the order contained item(s) with Associate category
elseif ($associate) {
$variable_text = 'company/organization';
$variable_text_short = 'organization';
$variable_text_mailto = 'company%2Forganization';
}
// OK, so if the order contained an item with the State or the Associate category, then do this...
// ... which will fill the content with certain variable text based on whether it was in State, Associate, or bothh
if ($state || $associate) {
echo '<h2>You\'re a Member!</h2>
<p>Thank you for your membership in ACME. Your membership extends to all individuals within your '.$variable_text.'.</p>
<p>Individuals within your '.$variable_text_short.' may register for a new user account here: <a href="https://acmeorg.wpengine.com/register/" target="_blank">https://acmeorg.wpengine.com/register/</a>. The password is <strong><code>ACME</code></strong>.</p>
<p>Existing users should review and update their account profile here: <a href="https://acmeorg.wpengine.com/login/" target="_blank">https://acmeorg.wpengine.com/login/</a>. Existing users will log in with their unique password.</p>
<p>To facilitate the new user registration process and existing user account update process, you may <strong><a href="mailto:?subject=Join%20us%20on%20ACME&body=Your%20'.$variable_text_mailto.'%20is%20a%20member%20of%20ACME.%20%0D%0A%0D%0AIf%20you%20have%20never%20had%20a%20ACME%20account,%20register%20here:%20https%3A%2F%2Facmeorg.wpengine.com%2Fregister%2F%20%0D%0A%0D%0AThe%20password%20is%20ACME%20%0D%0A%0D%0AIf%20you%20currently%20have%20a%20ACME%20account,%20log%20in%20now%20to%20review%20and%20update%20your%20account%20information%20here:%20https%3A%2F%2Facmeorg.wpengine.com%2Flogin%2F">click here to send an email to your colleagues</a>.</p>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment