hi and thanks for stopping by.
i am currently writing a plugin for wordpress. i need a button inside a certain page, that triggers an email-notification. i figured it would be good to use the woocommerce email functionality, since it is a customer email and i’d like to use the woocommerce-email-templates, too.
in my plugin i include my class extension via
function add_wc_custom_email( $email_classes ) {
require( 'includes/class-wc-custom-email.php' );
$email_classes['WC_Custom_Email'] = new WC_Custom_Email();
return $email_classes;
}
add_filter( 'woocommerce_email_classes', 'add_wc_custom_email' );
//if i write a print_r(something) inside this function, just to alert if this filters hits, i don't see a result, so i think the error is here.. the filter gets hit under woocommerce->settings->emails but not, if you simply load a page in the backend
in the file class-wc-custom-email.php i have my class extension
class WC_Custom_Email extends WC_Email {
public function __construct() {
â¦
}
public function mail( $var1, $var2, $var3 ) {
â¦
}
}
my ajax looks like this
( function( $ ) {
$('#button1').on( "click", function() {
$.post(
ajaxurl,
{
'action' : 'jnz_email_custom',
'page_id': '<?php the_ID(); ?>',
},
function ( response ) {
console.log( "triggered", response )
}
);
});
})(jQuery);
the ajax handler looks like this
add_action('wp_ajax_jnz_email_custom', 'jnz_email_custom');
function jnz_email_custom() {
$message = email_custom( $_POST['page_id'] );
$response = array(
'what' => 'email_custom_triggered',
'action' => 'email custom triggered',
'id' => $_POST['page_id'],
'data' => $message,
);
$xmlResponse = new WP_Ajax_Response( $response );
$xmlResponse->send();
}
the function that i trigger with ajax
function email_custom( $page_id = 2728 ) {
$field = get_field( 'something', $page_id )[0];
//get_field() is a function by plugin 'Advanced Custom Fields' and works fine
function somethingSomething( $field ) {
â¦
return $results;
}
foreach ( $results as $result ) :
$email = new WC_Custom_Email();
$email->mail( $result['details']['name'], $result['details']['email'], $result['something'] );
endforeach;
return json_encode( $results, JSON_UNESCAPED_UNICODE );
//this is the $xmlResponse i get from the ajax call and looks fine (but only works, if i uncomment the 'new WC_Custom_Email()' stuff. otherwise i see the "Fatal Error" thingy)
}
the response is: Fatal error: Class 'WC_Custom_Email' not found in â¦
all the functions work fine, if i return the $results
i see all i want and get no errors.
same is, when i change it to new WC_Email()
. so my guess is, that the woocommerce functionality isn’t loaded into my admin->edit_page_xy screen. so the big question is: how would i load the woocommerce functionality (or only the email-functionality) into my plugin..??
hope this is somewhat clear and makes any sense. i only know little php and am completely new to oop.
Ok, so this was a bit of a beast.
I’ve written a demo plugin with the following structure:
##The main plugin file
kia-ajax-email.php
This file is responsible for enqueuing the script, adding your custom email class, creating a button on the single product page, and registering an action as one that triggers a WooCommerce email:
##The email class
includes/class-wc-test-ajax-email.php
the script
js/script.js
The email templates. first
templates/emails/test.php
andtemplates/emails/plain/test.php
and
templates/emails/plain/test.php
Going forward
This sends an email when a button is clicked. You will need to modify where that button goes, and because it is kind of slow (or seems so on my local setup) I would definitely recommend some kind of spinner/notification that the action is happening. You’ll also need to customize what data is sent via ajax and how that ends up in the templates. This is just a proof of concept.