1
votes

I have 2 custom email mailers setup as wc_shipped_order & wc_received_order and it works great. I have the trigger set so when the status of an order is changed to "shipped" it triggers the shipped order email mailer.

I'm trying to add a Tracking shipping code through another custom plugin via the action hook woocommerce_email_before_order_table with the following:

add_action( 'woocommerce_email_before_order_table', 'tracking_number_email', 10, 4 );
function tracking_number_email( $order, $sent_to_admin, $plain_text, $email ) {
    if( 'wc_shipped_order_email' == $email->id ){
        echo '<p><strong>'.__('Australia Post Tracking Number').': </strong>' . get_post_meta( $order->get_id(), 'tracking_number', true ) . '</p>';
    }
}

However when I try and add an if statement to check if the mailer was wc_shipped_order, I get an error. The actual echo part works, but the IF statement is failing and throwing out this error:

Notice: Trying to get property 'id' of non-object in /var/www/wp-content/plugins/my-custom-functions/inc/php/functional.php(103) : eval()'d code on line 239

It has no idea what $email->id is and I've tried dumping that variables for $email but theres nothing there.

Can someone help me?

1

1 Answers

2
votes

In WooCommerce $email variable argument is expected to be an object where you can get some properties (like the id in your code). The error thrown tells that it's not an object, so it's not possible get any property from it (and so not the id property).

To avoid this problem, you could use PHP conditional function is_object() like:

add_action( 'woocommerce_email_before_order_table', 'tracking_number_email', 10, 4 );
function tracking_number_email( $order, $sent_to_admin, $plain_text, $email ) {
    if( is_object($email) && 'wc_shipped_order_email' == $email->id ){
        $tracking_number = get_post_meta( $order->get_id(), 'tracking_number', true );

        if ( ! empty($tracking_number) ) {
            echo '<p><strong>'.__('Australia Post Tracking Number').': </strong>' . $tracking_number . '</p>';
        }
    }
}

It should works now without error.

Now this problem can be related to a plugin, your theme or some other custom code made by you. With the provided information nobody can guess why this happen.