We have already set multiple email addresses based on cart qty. For example, if someone orders 4 items, they are required to enter the name, email, contact 4 times.
The problem is I want an email to be sent to all 4 people upon successful transaction.
Here is the code we got and it doesn't send to all 4 people:
add_filter( 'woocommerce_email_headers', 'woocommerce_new_order_email_bcc_copy', 10, 4 );
function woocommerce_new_order_email_bcc_copy( $headers, $email_id, $order, $email = null ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$i = 0;
if ( !empty($order) ) {
foreach($items as $item => $values) {
$i++;
if( !empty($email = $order->get_meta('patient_email'.$i)) ) { // replace with your meta key name
$names = $order->get_meta('patient_fullname'.$i);
$headers .= ' '.$names.' <'.$email.'>,' . "\r\n";
}
}
}return 'Cc: '.$headers;
}
Related
There are many threads which deal with the topic "custom fields in WooCommerce emails" but I couldn't find the exact case I am struggling with.
I could achieve 50% of this project to display the field as a meta in the order table.
add_action( 'woocommerce_order_item_meta_end', 'custom_product_info', 20, 4 );
function custom_product_info ( $item_id, $item, $order, $plain_text ) {
$id = $item->get_product_id();
$erlebnisidgc = get_field('erlebnis_id',$id);
if($erlebnisidgc){
echo "<p>Here's your Link</p><a href=https://b-ceed.de/?eventid='.$erlebnisidgc'>Testlink</a>";
}
}
So this code works perfectly with the custom field. Problem is that this output isn't shown only in the customer_completed_order email but also in all other emails.
Therefore, I tried this code snippet:
add_action( 'woocommerce_order_item_meta_end', 'custom_product_info', 20, 4 );
function custom_product_info ( $item_id, $item, $order, $plain_text ) {
if ( $email->id == 'customer_completed_order' ) {
$id = $item->get_product_id();
$erlebnisidgc = get_field('erlebnis_id',$id);
if($erlebnisidgc){
echo "<p>Here's your Link</p><a href=https://b-ceed.de/?eventid='.$erlebnisidgc'>Testlink</a>";
}
}
}
But now the output won't be displayed in any email anymore and it triggers a internal server error. Any advice?
$email ($email->id) is not passed as argument to the woocommerce_order_item_meta_end hook, therefore it is undefined
So to target specific email notifications a workaround will be needed, this can be done by creating a global variable via the woocommerce_email_before_order_table hook
So you get:
// Setting global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$GLOBALS['email_data'] = array(
'email_id' => $email->id, // The email ID (to target specific email notification)
'is_email' => true // When it concerns a WooCommerce email notification
);
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );
function action_woocommerce_order_item_meta_end( $item_id, $item, $order, $plain_text ) {
// Getting the custom 'email_data' global variable
$ref_name_globals_var = $GLOBALS;
// Isset & NOT empty
if ( isset ( $ref_name_globals_var ) && ! empty( $ref_name_globals_var ) ) {
// Isset
$email_data = isset( $ref_name_globals_var['email_data'] ) ? $ref_name_globals_var['email_data'] : '';
// NOT empty
if ( ! empty( $email_data ) ) {
// Target specific emails, several can be added in the array, separated by a comma
$target_emails = array( 'customer_completed_order' );
// Target specific WooCommerce email notifications
if ( in_array( $email_data['email_id'], $target_emails ) ) {
// Get product ID
$product_id = $item->get_product_id();
// Get field
$erlebnisidgc = get_field( 'erlebnis_id', $product_id );
// Has some value
if ( $erlebnisidgc ) {
echo '<p>Here is your Link</p>';
echo 'Testlink';
}
}
}
}
}
add_action( 'woocommerce_order_item_meta_end', 'action_woocommerce_order_item_meta_end', 10, 4 );
Used in this answer:
Determine only for WooCommerce admin email notification
How to target other WooCommerce order emails
I need to display customer Bio in WooCommerce admin order edit pages after the billing address.
Actually I only succeeded to display in a column like that:
With this code:
// Adding a custom new column to admin orders list
add_filter( 'manage_edit-shop_order_columns', 'custom_column_eldest_players', 20 );
function custom_column_eldest_players($columns)
{
$reordered_columns = array();
// Inserting columns to a specific location
foreach( $columns as $key => $column){
$reordered_columns[$key] = $column;
if( $key == 'order_status' ){
// Inserting after "Status" column
$reordered_columns['user-bio'] = __( 'Note client', 'woocommerce');
}
}
return $reordered_columns;
}
// Adding custom fields meta data for the column
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id ) {
if ( 'user-bio' === $column ) {
global $the_order;
echo ( $user = $the_order->get_user() ) ? $user->description : 'n/c';
}
}
But I don't know how to insert in WooCommerce admin order edit pages. Any advice?
Do display the user description on the admin order pages after billing adress you can use the woocommerce_admin_order_data_after_billing_address acton hook.
So you get:
// Display on admin order pages after billing adress
function action_woocommerce_admin_order_data_after_billing_address( $order ) {
// Get user
$user = $order->get_user();
// Initialize
$output = __( 'Bio: ', 'woocommerce' );
// Is a WP user
if ( is_a( $user, 'WP_User' ) ) {
! empty( $user->description ) ? $output .= $user->description : $output .= __( 'n/c', 'woocommerce' );
} else {
$output .= __( 'n/c', 'woocommerce' );
}
// Output
echo $output;
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );
I am trying to display a specific message on the Order confirmation email IF one of several products of your order is/are on backorder.
I am struggling to get the right function to scan all the products and get my boolean working.
My current code:
add_action( 'woocommerce_email_after_order_table', 'backordered_items_checkout_notice_email', 20, 4 );
function backordered_items_checkout_notice_email( $order, $sent_to_admin, $plain_text, $email ) {
$found2 = false;
foreach ( $order->get_items() as $item ) {
if( $item['data']->is_on_backorder( $item['quantity'] ) ) {
$found2 = true;
break;
}
}
if( $found2 ) {
if ( $email->id == 'customer_processing_order' ) {echo ' <strong>'.__('⌛ One or several products are Currently out of stock. <br/>Please allow 2-3 weeks for delivery.', 'plugin-mve').'</strong><br/>';}
}
}
With this code, when you click on "Order" the page just freezes and no email is sent. But I get the order in the backend.
Could anyone give me a hand to fix?
Your code contains a CRITICAL uncaught error, namely: Call to a member function is_on_backorder() on null
Following code will add the message for the customer_processing_order email notification. Also see: How to target other WooCommerce order emails
So you get:
function action_woocommerce_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {
// Initialize
$flag = false;
// Target certain email notification
if ( $email->id == 'customer_processing_order' ) {
// Iterating through each item in the order
foreach ( $order->get_items() as $item ) {
// Get a an instance of product object related to the order item
$product = $item->get_product();
// Check if the product is on backorder
if ( $product->is_on_backorder() ) {
$flag = true;
// Stop the loop
break;
}
}
// True
if ( $flag ) {
echo '<p style="color: red; font-size: 30px;">' . __( 'My message', 'woocommerce' ) . '</p>';
}
}
}
add_action( 'woocommerce_email_after_order_table', 'action_woocommerce_email_after_order_table', 10, 4 );
I'm using this code to generate an iframe if a customer has bought product 13372 and it works great:
<?php
// Get the current user data:
$user = wp_get_current_user();
$user_id = $user->ID; // Get the user ID
$customer_email = $user->user_email; // Get the user email
// OR
// $customer_email = get_user_meta( $user->ID, 'billing_email', true ); // Get the user billing email
// The conditional function (example)
// IMPORTANT: $product_id argument need to be defined
$product_id = 13372;
if( wc_customer_bought_product( $customer_email, $user_id, $product_id ) ) {
echo "You have additional listings!";
//iFrame goes here
} else {
echo "You have no additional listings.";
}
?>
Now I need to modify this to check how many times a user bought product ID 13372 and output that many number of iframes. If bought 3 times, output 3 iframes. I'm assuming a foreach loop, but what I've tried doesn't work. I followed this post: How to check how many times a product has been bought by a customer
But the example it doesn't return anything for me, not sure why!
Please try the following. We're looping through each completed shop order of a specific customer and putting all IDs of purchased products into an array. We then loop through that array to see how many times a specific ID appears. We store that value in a $count variable, which is used to determine how many times to output an iframe.
I've commented the code and made function and variable names as clear as possible.
<?php
function so58032512_get_product_purchase_quantity( $product_id ) {
// Array to hold ids of all products purchased by customer
$product_ids = [];
// Get all customer orders
$customer_orders = get_posts( array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed' // Only orders with status "completed"
) );
// Loop through each of this customer's order
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$items = $order->get_items();
// Loop through each product in order and add its ID to $product_ids array
foreach ( $items as $item ) {
$id = $item['product_id'];
array_push( $product_ids, $id );
}
}
// Variable to count times an ID exists in the $product_ids array
$count = 0;
// Loop through all of the IDs in our $product_ids array
foreach ( $product_ids as $key => $value ) {
// Every time the ID that we're checking against appears in the array
if ( $value == $product_id ) {
// Increment our counter
$count ++;
}
}
// Return the counter value that represents the number of times the
// customer bought a product with our passed in ID ($product_id)
return $count;
}
// Get the current user data:
$user = wp_get_current_user();
$user_id = $user->ID; // Get the user ID
$customer_email = $user->user_email; // Get the user email
// IMPORTANT: $product_id argument need to be defined
$product_id = 13372;
if ( wc_customer_bought_product( $customer_email, $user_id, $product_id ) ) {
$x = 1;
$number_of_iframes_to_show = so58032512_get_product_purchase_quantity( $product_id );
echo 'The customer bought product with ID ' . $product_id . ' ' . $number_of_iframes_to_show . ' time(s).<br>';
while ( $x <= $number_of_iframes_to_show ) {
echo "<br>Here's an iframe!<br>";
$x ++;
}
} else {
echo "You have no additional listings.";
}
?>
Please i find this code as solution for WooCommerce New order email manually trigger for some products but I don't know where exactly putting this code in functions.php or else thanks.
/**
* Modified from https://www.skyverge.com/blog/add-woocommerce-email-recipients-conditionally/
*
* Add another email recipient for admin New Order emails if a product from a specific category or with a specific tag is ordered
*
* #param string $recipient a comma-separated string of email recipients (will turn into an array after this filter!)
* #param \WC_Order $order the order object for which the email is sent
* #return string $recipient the updated list of email recipients
*/
function sv_conditional_email_recipient( $recipient, $order ) {
// Bail on WC settings pages since the order object isn't yet set yet
// Not sure why this is even a thing, but shikata ga nai
$page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
if ( 'wc-settings' === $page ) {
return $recipient;
}
// just in case
if ( ! $order instanceof WC_Order ) {
return $recipient;
}
$items = $order->get_items();
// check if product from category or with tag is in order
foreach ( $items as $item ) {
$product = $order->get_product_from_item( $item );
$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'names');
$product_cats = wp_get_post_terms( $product->get_id, 'product_cat', $args ); // could swap product_cat for product_tag
// add our extra recipient if there's a product from the category with slug "dieta" - commas needed!
// we can bail if we've found one, no need to add the recipient more than once
if ( $product && in_array( "dieta", $product_cats ) ) {
$recipient .= ', notify#example.com';
return $recipient;
}
}
return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );
I code this one and it works on my website, don't forget to replace the_name_of_the_cat by the name of the category and first_email#domain.com,second_email#domain.com by the two emails.
function change_email_recipient_depending_of_cat ( $recipient, $order ) {
global $woocommerce;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
if ( has_term( 'the_name_of_the_cat', 'product_cat', $product_id ) ) {
$recipient = 'first_email#domain.com,second_email#domain.com';
}
return $recipient;
}
}
add_filter( 'woocommerce_email_recipient_new_order', 'change_email_recipient_depending_of_cat', 10, 2 );