Programmatically Sending Email Using Woocommerce Template
-
I am working on creating a custom Order Action with a custom field called Estimated Ship Date. When viewing an order in the admin, it’s checking to see if a staff member has input an Estimated Ship Date for the order, if so it enables the custom Order Action.
When the custom Order Action is selected from the dropdown, I need it to send an email to billing email address with the Estimated Ship Date in the email.
I created a custom email template which has the code in it, but the issue I’m running into is actually triggering the email to send programmatically.
This is the full code I’m using in my plugin.
/* ############################################ Estimated Ship Date ############################################ */ /** * Add a custom action to order actions select box on edit order page * * @param array $actions order actions array to display * @return array - updated actions */ function bhi_wc_add_order_meta_box_action( $actions ) { global $theorder; // bail if the order does not have an estimated ship date if ( ! get_field('estimated_ship_date') ) { return $actions; } // add "estimated ship date" custom action $actions['wc_custom_order_action'] = __( 'Send Estimated Ship Date Email', 'bhi-textdomain' ); return $actions; } add_action( 'woocommerce_order_actions', 'bhi_wc_add_order_meta_box_action' ); /** * Send Email to Customer with Estimated Ship Date * * @param \WC_Order $order */ function bhi_wc_process_order_meta_box_action( $order ) { function get_custom_email_html( $order, $heading = false, $mailer ) { $template = 'emails/estimated-ship-date.php'; return wc_get_template_html( $template, array( 'order' => $order, 'email_heading' => $heading, 'sent_to_admin' => false, 'plain_text' => false, 'email' => $mailer ) ); } // load the mailer class $mailer = WC()->mailer(); //format the email $recipient = $order->get_billing_email(); $subject = __("Estimated Ship Date Assigned", 'theme_name'); $content = get_custom_email_html( $order, $subject, $mailer ); $headers = "Content-Type: text/html\r\n"; //send the email through wordpress $mailer->send( $recipient, $subject, $content, $headers ); // Add Order Note indicated email has been sent // translators: Placeholders: %s is a user's display name $message = sprintf( __( 'Estimated shipment date email has been sent by %s', 'bhi-textdomain' ), wp_get_current_user()->display_name ); $order->add_order_note( $message ); } add_action( 'woocommerce_order_action_wc_custom_order_action', 'bhi_wc_process_order_meta_box_action' );This is the code I’m using in my custom email template:
<?php /** * Estimated Ship Date email * * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-note.php. * * * @see https://docs.woocommerce.com/document/template-structure/ * @author Bluehive Interactive * @package WooCommerce/Templates/Emails * @version 2.5.0 */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * @hooked WC_Emails::email_header() Output the email header */ do_action( 'woocommerce_email_header', $email_heading, $email ); ?> <p><?php _e( "Hello, An estimated shipment date has been added to your order:", 'woocommerce' ); ?></p> <p><?php $bhi_estimated_ship_date = get_field('estimated_ship_date', $order_id);?> <strong>Estimated Ship Date:</strong> <?php echo $bhi_estimated_ship_date;?></p> <p><?php _e( "For your reference, your order details are shown below.", 'woocommerce' ); ?></p> <?php /** * @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Structured_Data::generate_order_data() Generates structured data. * @hooked WC_Structured_Data::output_structured_data() Outputs structured data. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); /** * @hooked WC_Emails::order_meta() Shows order meta data. */ do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email ); /** * @hooked WC_Emails::customer_details() Shows customer details * @hooked WC_Emails::email_address() Shows email address */ do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email ); /** * @hooked WC_Emails::email_footer() Output the email footer */ do_action( 'woocommerce_email_footer', $email );
The topic ‘Programmatically Sending Email Using Woocommerce Template’ is closed to new replies.