Last active
May 22, 2020 07:41
-
-
Save damiencarbery/4a7d97fd58b262a1b1c97c4132d645ea to your computer and use it in GitHub Desktop.
Code to answer question on my blog: https://www.damiencarbery.com/2018/07/add-info-to-woocommerce-order-emails/#comment-8852
This file contains 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 | |
/* | |
Plugin Name: Custom note to order email (for variation) | |
Plugin URI: https://www.damiencarbery.com/ | |
Description: Add a note to the order email if the order contains products with a specific variation name. | |
Author: Damien Carbery | |
Version: 0.1 | |
*/ | |
/* Add note about electronic licenses to the order email. */ | |
add_action( 'woocommerce_email_order_details', 'dcwd_electronic_license_info_to_order_email', 5, 4 ); | |
function dcwd_electronic_license_info_to_order_email( $order, $sent_to_admin, $plain_text, $email ) { | |
// Only customers need to know about the delivery times. | |
if ( $sent_to_admin ) { | |
return; | |
} | |
$variation_name = 'White'; | |
$order_has_specified_variation = false; | |
$items = $order->get_items(); | |
foreach ( $items as $item_id => $item ) { | |
$product = $item->get_product(); | |
if ( $product->is_type( 'variation' ) ) { | |
$variation_suffix = ' ' . $variation_name; | |
$variation_strlen = strlen( $variation_suffix ); | |
if ( $variation_suffix == substr( $product->get_name(), -1 * $variation_strlen, $variation_strlen ) ) { | |
$order_has_specified_variation = true; | |
} | |
} | |
} | |
if ( $order_has_specified_variation ) { | |
if ( $plain_text ) { | |
echo "\nNOTE: This order has a product with the specified variation.\n"; | |
} | |
else { | |
echo '<p><strong>Note</strong>: This order has a product with the specified variation.</p>'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment