Last active
May 30, 2024 19:42
-
-
Save Garconis/634d23d0991f663ba0f2ab54bb624cdc to your computer and use it in GitHub Desktop.
WooCommerce | Add custom text to the emails based on the status of the order and if a product ID is in the order
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 | |
/************** | |
DIFFERENT MESSAGES FOR DIFFERENT PRODUCTS | |
****************/ | |
//hook our function to the new order email | |
add_action('woocommerce_email_order_details', 'email_order_details_products', 1, 4); | |
function email_order_details_products($order, $admin, $plain, $email) { | |
// getting the status of the current order | |
$status = $order->get_status(); | |
// checking if it's the order status we want (note, this is for the status of the order ... not necessarily WHAT EMAIL is being sent | |
if ( ($status == "completed") || ($status == "processing") || ($status == "on-hold") ) { | |
// the ID of the conference product | |
$prod_arr = array( 454 ); | |
// getting the order products | |
$items = $order->get_items(); | |
// starting the bought products variable | |
$bought = false; | |
// loop through each of the items in the order | |
foreach ( $items as $item ) { | |
// in case we want to access stuff about produdct ids of the ordered items (not currently used) | |
$product_id = $item['product_id']; | |
// Here you get your data from products | |
$custom_field_to_check = get_post_meta( $product_id, '_tmcartepo_data', true); | |
// checking each item to see if any of the ordered products is matching the conference product id | |
if ( in_array( $item['product_id'], $prod_arr ) ) { | |
// if the product is found in the order, then we say it was bought | |
$bought = true; | |
} | |
} | |
if ( $bought ) { | |
// custom text for the email | |
echo __( '<h2>Custom Title</h2> | |
<p>Hey it\'s your text right here with <a href="https://example.com" target="_blank">a link</a> too!</p>', 'fswc' ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment