Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicomollet/1d0d1e80c4243644f444382658ce6968 to your computer and use it in GitHub Desktop.
Save nicomollet/1d0d1e80c4243644f444382658ce6968 to your computer and use it in GitHub Desktop.
Gravity Forms: Use WooCommerce emails templates for notifications
<?php
/**
* Gravity Forms: Use WooCommerce emails templates for notifications.
*
* @param string $template
*
* @return string
*/
public function custom_gform_html_message_template_pre_send_email( string $template ) {
if ( function_exists( 'wc_get_template_html' ) && class_exists( 'WC_Email' ) ) {
$header = wc_get_template_html(
'emails/email-header.php',
array(
'email_heading' => '{subject}',
)
);
$footer = wc_get_template_html( 'emails/email-footer.php' );
$wc_email = new WC_Email();
$template = $wc_email->style_inline( $header . '{message}' . $footer );
}
return $template;
}
add_filter( 'gform_html_message_template_pre_send_email', 'custom_gform_html_message_template_pre_send_email', 20, 1 );
@robertosanval
Copy link

Thanks for the snippet!

I've made a simple improvement

WC_Email class is not present until you init WC_Emails class.

function custom_gform_html_message_template_pre_send_email(string $template) {
    $wcEmails = new WC_Emails();
    $wcEmails->init();

    if (function_exists('wc_get_template_html') && class_exists('WC_Email')) {
        $header = wc_get_template_html(
            'emails/email-header.php',
            [
                'email_heading' => '{subject}',
            ]
        );

        $footer = wc_get_template_html('emails/email-footer.php');

        $wc_email = new WC_Email();
        $template = $wc_email->style_inline($header . '{message}' . $footer);
    }

    return $template;
}
add_filter('gform_html_message_template_pre_send_email', 'custom_gform_html_message_template_pre_send_email', 20, 1);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment