0

In a plugin I'm working on, I'm trying to replace the OOTB customer-completed-order email with a template in the plugin

Constructor:

define( 'BKF_WC_EMAIL_PATH', plugin_dir_path( __FILE__ ) );
add_filter('wc_get_template', array($this, 'bkf_customer_completed_order_template'), PHP_INT_MAX, 5);

Function:

    function bkf_customer_completed_order_template($template, $template_name, $args, $template_path, $default_path) {
        if( $template_name == 'emails/customer-completed-order.php' ) {
            $template = trailingslashit(BKF_WC_EMAIL_PATH) . 'templates/' . $template_name;
            return $template;
        }
    }

note the template is still pulling the default woo one

Any thoughts/ideas are welcome!

  • I am unsure if you have debugging ON or not, but you should get an error from that code as the `woocommerce_locate_template` filter hook returns only 3 params and you have defined `$accepted_args` as **4**, Please fix this thing and enable debugging and look for errors as well. – Vijay Hardaha Dec 20 '22 at 08:55
  • Thanks for pointing that out! Have fixed the number of args - no change in the outcome there unfortunately. I've been running it with debug and it's not showing anything other than "CUSTOM_WC_EMAIL_PATH is already defined" at the line where i define it – Scott - BAKKBONE Australia Dec 20 '22 at 14:36
  • So, whether I use the deprecated `woocommerce_locate_template` or the current `wc_get_template` filter, I've tried adding `error_log('Hello world!');` outside of the `if()` statement but nothing is getting logged when I load the template's settings page and/or view the template on that page - implying to me that the filter is not firing. – Scott - BAKKBONE Australia Jan 16 '23 at 07:43

1 Answers1

0

Worked it out!

Instead of the method used in my original question, here's what worked for me:

I created a new class (similar to woocommerce/includes/emails/class-wc-email-customer-completed-order.php) - for demo purposes we'll call it My_Custom_Class

I then called like so in my constructor for the parent class I was working on:

add_action('woocommerce_email_classes', array( $this, 'bk_register_email' ), PHP_INT_MAX, 1 );

And added this function:

public function bk_register_email( $emails ) {
        require_once 'emails/my-custom-class.php';

        $emails['WC_Email_Customer_Completed_Order'] = new My_Custom_Class();

        return $emails;
    }