I’ve followed some tutorials and thread here, But I’ve surely missed something.
Here is the code used (I’ve removed all which is not concerned):
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WC_Email' ) ) {
return;
}
/**
* Class WC_Customer_Failed_Order
*/
class WC_Customer_Invoice_Order extends WC_Email {
public $resp2;
/**
* Create an instance of the class.
*
* @access public
* @return void
*/
public function __construct() {
// Email slug we can use to filter other data.
$this->id = 'wc_customer_invoice_order';
$this->title = __( 'An invoice is available', 'textdomain' );
$this->description = __( 'An e-mail is sent when an invoice is left on an order.', 'textdomain' );
// For admin area to let the user know we are sending this email to customers.
$this->customer_email = true;
$this->heading = __( 'An invoice is available', 'textdomain' );
// translators: placeholder is {blogname}, a variable that will be substituted when email is sent out.
$this->subject = sprintf( _x( '[%s] Invoice available', 'Objet par défaut pour les emails des commandes échouées destinés aux clients', 'textdomain' ), '{blogname}' );
// Template paths.
$this->template_html = 'emails/wc-customer-notice-invoice-order.php';
$this->template_plain = 'emails/plain/wc-customer-notice-invoice-order.php';
$this->template_base = PLUGIN_PATH . 'templates/';
// Manual email notification
add_action( 'woocommerce_order_actions', array( $this, 'wc_add_order_meta_box_action' ) );
add_action( 'woocommerce_order_action_send_invoice_notification', array( $this, 'trigger' ), 10, 2 );
parent::__construct();
}
/**
* Get content html.
*
* @access public
* @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,
),
'',
$this->template_base
);
}
/**
* 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,
),
'',
$this->template_base
);
}
//https://gist.github.com/bekarice/5233ed58c3a836064123b290463241c0
public function wc_add_order_meta_box_action( $actions ) {
// add "custom notif" custom action
$actions['send_invoice_notification'] = __( 'Send notification', 'textdomain' );
return $actions;
}
public function trigger( $order_id, $order ){
error_log( sprintf( 'email triggered %s', $order->get_id() ) );
if ( ! empty( $_POST['acf']['field_61ab6dd460813'] ) ) {
foreach ( $_POST['acf']['field_61ab6dd460813'] as $key => $value ) {
$already_sent = get_post_meta( $order_id, $key, true );
if ( empty( $already_sent ) ) {
$this->object = wc_get_order( $order_id );
if ( version_compare( '3.0.0', WC()->version, '>' ) ) {
$order_email = $this->object->billing_email;
} else {
$order_email = $this->object->get_billing_email();
}
$this->recipient = $order_email;
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
return;
}
$return = $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
if ( $return ) {
update_post_meta( $order_id, $key, '1' );
}
}
}
}
return false;
}
}
and here the plugin main file:
<?php
/**
* Plugin Name: MyPlugin
**/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly.
class myClass {
public function __construct() {
add_filter( 'woocommerce_email_classes', array( $this, 'register_email' ) );
}
/**
* @param array $emails
*
* @return array
*/
public function register_email( $emails ) {
require 'inc/class-wc-customer-notice-invoice-order.php';
$emails['WC_Customer_Invoice_Order'] = new WC_Customer_Invoice_Order();
return $emails;
}
}
new myClass();
When I try to send a notification, the hook "woocommerce_order_action_send_invoice_notification" is well played but I got an error "WC_Email" class not found.
Do you have any idea what’s wrong ?