最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Change WooCommerce Email Header using custom plugin

programmeradmin1浏览0评论

I want to change the woocommerce email header template with a new one so that I could add conditions in header template to get value from the dashboard (To change the color of header based on user input from the dashboard). I have created a plugin file to do so.

I have followed several tutorials and all I received is a bunch of errors. I have a class with the following code:

public function __construct(){
    add_action('woocommerce_email',array($this,'woocommerce_email'));
}

Now I have added code to remove default header hook

public function woocommerce_email($mailer){

    remove_action('woocommerce_header',array($mailer,'email_header'));
    add_action('woocommerce_header',array($this,'email_header'));

now calling the template

public function email_header() {          
    wc_get_template( 'emails/email-header.php');
}

I am not passing anything to the template file. So no parameters are passed to functions. I just wanted to see that my template is being taken. Also, I assume $mailer to be a part of the woocommerce class. Any help would be appreciated.

Please note that this is a plugin functionality so I am not interested in replacing the woocommerce email templates.

I want to change the woocommerce email header template with a new one so that I could add conditions in header template to get value from the dashboard (To change the color of header based on user input from the dashboard). I have created a plugin file to do so.

I have followed several tutorials and all I received is a bunch of errors. I have a class with the following code:

public function __construct(){
    add_action('woocommerce_email',array($this,'woocommerce_email'));
}

Now I have added code to remove default header hook

public function woocommerce_email($mailer){

    remove_action('woocommerce_header',array($mailer,'email_header'));
    add_action('woocommerce_header',array($this,'email_header'));

now calling the template

public function email_header() {          
    wc_get_template( 'emails/email-header.php');
}

I am not passing anything to the template file. So no parameters are passed to functions. I just wanted to see that my template is being taken. Also, I assume $mailer to be a part of the woocommerce class. Any help would be appreciated.

Please note that this is a plugin functionality so I am not interested in replacing the woocommerce email templates.

Share Improve this question edited Apr 19, 2019 at 14:08 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Nov 9, 2017 at 12:14 melvinmelvin 2981 gold badge5 silver badges18 bronze badges 4
  • 1 you don't need to unhook / rehook. just create a file in your theme directory in woocommerce/emails/email-header.php and the original e-mail submission will use it – mmm Commented Nov 9, 2017 at 13:10
  • But i am trying to create a plugin. Not a theme development. This plugin is used for all themes. So instead of taking email templates from woocommerce plugin, it has to take custom templates created in my plugin directory – melvin Commented Nov 9, 2017 at 16:45
  • Then in the method email_header of your class, just display the file of your plugin, with require e.g. – mmm Commented Nov 9, 2017 at 17:18
  • its not working – melvin Commented Nov 9, 2017 at 17:44
Add a comment  | 

2 Answers 2

Reset to default 2

You have to remove action woocommmerce_email_header

remove_action( 'woocommerce_email_header', 'email_header');

Which was registered at

wp-content\plugins\woocommerce\includes\class-wc-emails.php

Than add your own action

add_action('woocommerce_email_header','your_function_name');

your function should output html

default there is below code in header template

<?php
/**
 * Email Header
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/email-header.php.
 *
 * 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/document/template-structure/
 * @author  WooThemes
 * @package WooCommerce/Templates/Emails
 * @version 2.4.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=<?php bloginfo( 'charset' ); ?>" />
        <title><?php echo get_bloginfo( 'name', 'display' ); ?></title>
    </head>
    <body <?php echo is_rtl() ? 'rightmargin' : 'leftmargin'; ?>="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">
        <div id="wrapper" dir="<?php echo is_rtl() ? 'rtl' : 'ltr'?>">
            <table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">
                <tr>
                    <td align="center" valign="top">
                        <div id="template_header_image">
                            <?php
                                if ( $img = get_option( 'woocommerce_email_header_image' ) ) {
                                    echo '<p style="margin-top:0;"><img src="' . esc_url( $img ) . '" alt="' . get_bloginfo( 'name', 'display' ) . '" /></p>';
                                }
                            ?>
                        </div>
                        <table border="0" cellpadding="0" cellspacing="0" width="600" id="template_container">
                            <tr>
                                <td align="center" valign="top">
                                    <!-- Header -->
                                    <table border="0" cellpadding="0" cellspacing="0" width="600" id="template_header">
                                        <tr>
                                            <td id="header_wrapper">
                                                <h1><?php echo $email_heading; ?></h1>
                                            </td>
                                        </tr>
                                    </table>
                                    <!-- End Header -->
                                </td>
                            </tr>
                            <tr>
                                <td align="center" valign="top">
                                    <!-- Body -->
                                    <table border="0" cellpadding="0" cellspacing="0" width="600" id="template_body">
                                        <tr>
                                            <td valign="top" id="body_content">
                                                <!-- Content -->
                                                <table border="0" cellpadding="20" cellspacing="0" width="100%">
                                                    <tr>
                                                        <td valign="top">
                                                            <div id="body_content_inner">

For someone doing this from your theme.

  1. Copy the contents of the email template that you can find here:

https://github/woocommerce/woocommerce/blob/master/templates/emails/email-header.php

  1. Paste the contents of the file in 1. in the following path

yourtheme/woocommerce/emails/email-header.php

  1. Optional if you need the order object to get a data your need to add the following in your functions.php
    add_action( 'woocommerce_email_header', 'email_header_before', 1, 2 );
    function email_header_before( $email_heading, $email ){
        $GLOBALS['email'] = $email;
    }
  1. Optional, and then in the template file you just create it use it this way
// Call the global WC_Email object
global $email;

// Get an instance of the WC_Order object
$order = $email->object;

Step 3 and 4, obtained from this answer

https://stackoverflow/questions/49739353/get-the-order-object-in-woocommerce-email-header-template

by @LoicTheAztec

发布评论

评论列表(0)

  1. 暂无评论