Forked from helgatheviking/class-wc-kia-class-email.php
Created
May 11, 2022 19:37
-
-
Save cdsaenz/f0171d8c983f8804d8ba4b01ef010fd4 to your computer and use it in GitHub Desktop.
Send a WooCommerce Customer email from the Order Admin Actions
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 | |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
if ( ! class_exists( 'WC_KIA_Class_Email', false ) ) : | |
/** | |
* A custom Order WooCommerce Email class | |
* | |
* @since 0.1 | |
* @extends \WC_Email | |
*/ | |
class WC_KIA_Class_Email extends WC_Email { | |
/** | |
* Set email defaults | |
* | |
* @since 0.1 | |
*/ | |
public function __construct() { | |
// set ID, this simply needs to be a unique name | |
$this->id = 'wc_zlp_class'; | |
$this->customer_email = true; | |
// this is the title in WooCommerce Email settings | |
$this->title = __( 'Test Order Email', 'your-plugin-textdomain' ); | |
// this is the description in WooCommerce email settings | |
$this->description = __( 'Test Notification emails are sent when a customer places an order switches to processing', 'your-plugin-textdomain' ); | |
// these are the default heading and subject lines that can be overridden using the settings | |
$this->heading = __( 'Test Order Email', 'your-plugin-textdomain' ); | |
$this->subject = __( 'Test Order Email', 'your-plugin-textdomain' ); | |
// these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar | |
$this->template_html = 'emails/customer-processing-order.php'; | |
$this->template_plain = 'emails/plain/customer-processing-order.php'; | |
$this->placeholders = array( | |
'{order_date}' => '', | |
'{order_number}' => '', | |
); | |
// Triggers for this email. | |
//add_action( 'woocommerce_order_status_cancelled_to_processing_notification', array( $this, 'trigger' ), 10, 2 ); | |
//add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ), 10, 2 ); | |
//add_action( 'woocommerce_order_status_on-hold_to_processing_notification', array( $this, 'trigger' ), 10, 2 ); | |
//add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ), 10, 2 ); | |
// Manually send via admin order actions. | |
add_action( 'woocommerce_order_action_kia_email_class_detail_action', array( $this, 'trigger' ) ); | |
// Call parent constructor to load any other defaults not explicity defined here | |
parent::__construct(); | |
} | |
/** | |
* Get email subject. | |
* | |
* @since 3.1.0 | |
* @return string | |
*/ | |
public function get_default_subject() { | |
return __( 'Your {site_title} order is totally happening!', 'your-plugin-textdomain' ); | |
} | |
/** | |
* Get email heading. | |
* | |
* @since 3.1.0 | |
* @return string | |
*/ | |
public function get_default_heading() { | |
return __( 'You deserve all the tacos', 'your-plugin-textdomain' ); | |
} | |
/** | |
* Trigger the sending of this email. | |
* | |
* @param int $order_id The order ID. | |
* @param WC_Order|false $order Order object. | |
*/ | |
public function trigger( $order_id, $order = false ) { | |
$this->setup_locale(); | |
if ( $order_id && ! is_a( $order, 'WC_Order' ) ) { | |
$order = wc_get_order( $order_id ); | |
} | |
if ( is_a( $order, 'WC_Order' ) ) { | |
$this->object = $order; | |
$this->recipient = $this->object->get_billing_email(); | |
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() ); | |
$this->placeholders['{order_number}'] = $this->object->get_order_number(); | |
} | |
if ( $this->is_enabled() && $this->get_recipient() ) { | |
$success = $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); | |
if( $success ) { | |
// ADD THE ORDER NOTE | |
$message = sprintf( __( 'Class detail for "%s" is sent.', 'zlp_class' ), $order->get_meta( 'class_name', true ) ); | |
$order->add_order_note( $message ); | |
// ADD THE FLAG SO THIS ACTION WON'T BE SHOWN AGAIN (I don't really now if that's good... you might need to resend it) | |
// $order->update_meta_data( '_wc_order_class_detail_sent', 'yes' ); | |
$order->save(); | |
} | |
} | |
$this->restore_locale(); | |
} | |
/** | |
* Get content html. | |
* | |
* @return string | |
*/ | |
public function get_content_html() { | |
return wc_get_template_html( | |
$this->template_html, | |
array( | |
'order' => $this->object, | |
'email_heading' => $this->get_heading(), | |
'additional_content' => $this->get_additional_content(), | |
'sent_to_admin' => false, | |
'plain_text' => false, | |
'email' => $this, | |
) | |
); | |
} | |
/** | |
* Get content plain. | |
* | |
* @return string | |
*/ | |
public function get_content_plain() { | |
return wc_get_template_html( | |
$this->template_plain, | |
array( | |
'order' => $this->object, | |
'email_heading' => $this->get_heading(), | |
'additional_content' => $this->get_additional_content(), | |
'sent_to_admin' => false, | |
'plain_text' => true, | |
'email' => $this, | |
) | |
); | |
} | |
/** | |
* Default content to show below main email content. | |
* | |
* @since 3.7.0 | |
* @return string | |
*/ | |
public function get_default_additional_content() { | |
return __( 'Thanks for using {site_address}!', 'your-plugin-textdomain' ); | |
} | |
} // end \WC_KIA_Class_Email class | |
endif; | |
return new WC_KIA_Class_Email(); |
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: Example Manual WooCommerce Email | |
* Plugin URI: https://gist.github.com/helgatheviking/cf3483038995dff909d1229b3e844ce5 | |
* Donation URL: https://paypal.me/helgatheviking | |
* Description: Send a customer email from the Order Admin Actions. | |
* Version: 1.0 | |
* Author: Kathy Darling | |
* Author URI: http://kathyisawesome.com | |
* Requires at least: 5.0.0 | |
* WC requires at least: 4.0.0 | |
* Tested up to: 5.3.0 | |
* WC tested up to: 4.1.0 | |
* | |
* Text Domain: wc_name_your_price | |
* Domain Path: /languages/ | |
* | |
* Copyright: © 2012 Kathy Darling. | |
* License: GNU General Public License v3.0 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
* | |
*/ | |
defined( 'ABSPATH' ) || exit; | |
/** | |
* Add a custom email to the list of emails WooCommerce should load | |
* | |
* @since 0.1 | |
* @param array $email_classes available email classes | |
* @return array filtered available email classes | |
*/ | |
function kia_add_order_woocommerce_email( $email_classes ) { | |
// add the email class to the list of email classes that WooCommerce loads | |
$email_classes['WC_ZLP_Class_Email'] = require_once( 'class-wc-kia-class-email.php' ); | |
return $email_classes; | |
} | |
add_filter( 'woocommerce_email_classes', 'kia_add_order_woocommerce_email' ); | |
/** | |
* Add a custom email to the list of emails WooCommerce should load | |
* | |
* @since 0.1 | |
* @param array $email_classes available email classes | |
* @return array filtered available email classes | |
*/ | |
function kia_wc_add_order_meta_box_action( $actions ) { | |
global $theorder; | |
// ADD "Email class details" CUSTOM ACTION | |
$actions['kia_email_class_detail_action'] = __( 'Email class details', 'kia_class' ); | |
return $actions; | |
} | |
add_action( 'woocommerce_order_actions', 'kia_wc_add_order_meta_box_action' ); | |
/** | |
* Register 'woocommerce_order_action_kia_email_class_detail_action' as an email trigger | |
* | |
* @param array $actions actions that trigger emails | |
* @return array | |
* | |
*/ | |
function kia_custom_woocommerce_email_actions( $actions ){ | |
$actions[] = 'woocommerce_order_action_kia_email_class_detail_action'; | |
return $actions; | |
} | |
add_filter( 'woocommerce_email_actions', 'kia_custom_woocommerce_email_actions' ); | |
function action_test() { | |
error_log('action does run'); | |
} | |
add_action( 'woocommerce_order_action_kia_email_class_detail_action', 'action_test' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment