trying to create custom woocomerce email not working from within a plugin
trying to create custom woocomerce email not working from within a plugin
So I am trying to create a custom email for my woocommerce store, that I can use as a payment reminder. I put everything together as follows: one "custom-payment-reminder.php" file, content as follows:
<?php
/**
* Payment reminder Email
*
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates\Emails
* @version 3.7.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WC_Custom_Payment_Reminder_Email extends WC_Email {
/**
* Constructor.
*/
public function __construct() {
$this->id = 'custom_payment_reminder';
$this->title = __('Payment Reminder', 'your-textdomain');
$this->description = __('This is a custom payment reminder email.', 'your-textdomain');
$this->template_html = 'emails/custom-payment-reminder-html.php';
$this->template_plain = 'emails/plain/payment-reminder.php';
// Call parent constructor
parent::__construct();
// Other settings like 'enabled', 'recipient', etc. can be added here
}
public function get_default_subject() {
return __( 'Zahlungserinnerung Ihrer Bestellung #{order_number} bei {site_url}' );
}
/**
* Trigger function to send the custom payment reminder email.
*
* @param int $order_id Order ID.
*/
public function trigger($order_id) {
// Get the order object
$order = wc_get_order($order_id);
// Set the email recipient(s)
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->recipient = $order->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
}
// Optionally, you can set additional email headers or parameters
$this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
}
/**
* 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,
)
);
}
}
Then I put an html template for the email in my child theme at woocommerce/emails/custom-payment-reminder-html.php". The content of that file shouldn't be important for this issue as it is identical in both cases.
Next I put the following lines in my functions.php:
add_filter('woocommerce_email_classes', 'add_custom_email_class');
function add_custom_email_class($email_classes) {
require_once(get_stylesheet_directory() . '/woocommerce/emails/custom-payment-reminder.php');
// Add your custom email class
$email_classes['WC_Custom_Payment_Reminder_Email'] = new WC_Custom_Payment_Reminder_Email();
return $email_classes;
}
And there you go, this works just fine. If I check at woocommerce -> settings -> email I can find the template, and when I click on it I can configure it and at the bottom under "html template" the path to the html template is shown.
Now comes the problem: I created a folder in the plugin folder, and created a new file containing the lines from the functions.php (see above). I moved the custom-payment-reminder.php file to the includes folder with that plugin folder and changed the path to the html template file to: $this->template_html = get_stylesheet_directory() . '/woocommerce/emails/custom-payment-reminder-html.php'; After activating the plugin the mail again show fine under woocommerce settings, but when clicking on it, at the bottom under "html template" it says "file not found".
I tried changing the path to the html template file, but still the template file can't be found for some reason. Anyone got a clue where to look for the error? Thanks a lot in advance!
Update: If from the plugin, I load the file with the email class from the child theme directory, it works! Now the question is why? I is probably something about the path to the html template, but I can't figure out what the problem is :-( It probably needs to be specified relative to the file calling it?
Moving functionality from the functions.php to a separate plugin. Accessing files does not work anymore
I think I just found the solution: The template base needs to be set like this (last row):
$this->template_html = 'emails/custom-payment-reminder-html.php';
$this->template_plain = 'emails/plain/payment-reminder.php';
$this->template_base = CUSTOM_WC_EMAIL_PATH . 'templates/';
Now it actually works! I do not understand though, why it didn't work with the absolute paths I used before.
December 30, 2023
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
December 30, 2023