Nerd in Action Code is poetry

Get current order object in email footer template

The current order object is not available in email-footer.php.

Analysis

If you check out the email_footer function in class-wc-emails.php, you will find it does not pass the order object into the template, which makes sense. The email-footer.php is used in all the email templates including customer-new-account.php.

Digging deeper, you will find that in the order template files, they do have pass the email object(WC_Email) to the woocommerce_email_footer action hook. For example, admin-new-order.php, and the order object is available at $email->object.

Solution

Step 1

Set a global variable wc_email(You may name it whatever you want) to reference the current email object in the woocommerce_email_footer action hook. Be sure to set the priority less than 10, so that it runs before woocommerce’s email_footer function.

// In your theme's functions.php

/**
 * Make sure the email instance is available in email-footer template
 */
add_action('woocommerce_email_footer', function ($email) {
    if (!isset($GLOBALS['wc_email'])) {
        $GLOBALS['wc_email'] = $email;
    }
}, 5, 1);

Step 2

Reference it in your email-footer.php

// In your theme's woocommerce/emails/email-footer.php

$order = null;

if (isset($GLOBALS['wc_email']) && $GLOBALS['wc_email']->object && $GLOBALS['wc_email']->object instanceof WC_Order) {
    $order = $GLOBALS['wc_email']->object;
}

Happy hacking!

More...

Add tinyMCE to Wordpress bulk edit

When adding the tinyMCE using wp_editor to bulk edit form, the Visual tab does not work.

More...

A few errors occured when installing a cordova plugin

TypeError: Object.keys called on non-object

More...

Weird PHP errors

After I deployed a new version onto our QA server, I was getting errors like Cannot redeclare class and Class XXX not found in a 3rd-party lib.

Weird, I haven’t made any changes in those files.

After hours of investigation, I found out that it was APC on the server which caused this issue, and solved it by restarting apache.

More...

Cannot resolve symbol 'CordovaActivity'

After I imported a cordova project into Android Studio, symbols of cordova classes cannot be resolved.

More...