21

I've just upgraded my local WooCommerce website to 3.0. Everything works perfectly as normal, but I've noticed with debugging turned on that I'm getting hundreds of the following notices:

[05-Apr-2017 12:25:00 UTC] PHP Notice: id was called <strong>incorrectly</strong>. Order properties should not be accessed directly. Please see <a href="https://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a> for more information. (This message was added in version 3.0.) in C:\xampp\htdocs\dev\wp-includes\functions.php on line 4137

So it looks like WooCommerce are pulling back being able to directly call order data. One example this code is being triggered by is this function in my functions.php file:

function eden_woocommerce_order_number($original, $order)
{
    return 'EDN-' . str_pad($order->id, 10, 0, STR_PAD_LEFT);
}

This function simply adds "EDN" to the start of the order ID and pads it by 10 characters, but WooCommerce doesn't like how I'm calling $order - what would be the best way to rewrite such a function that 3.0 is happy with?

Liam McArthur
  • 1,033
  • 3
  • 18
  • 42

3 Answers3

38

it says "id was called incorrectly. Order properties should not be accessed directly."

Try $order->get_id()

mbg
  • 448
  • 4
  • 7
  • 1
    Correct, this appeared to fix the problem. Also, I was incorrectly calling order data such as `$data = get_post_meta($order_id)`. Instead, I changed this to `$data = new WC_Order($order_id);` and called the necessary functions via that method, for example: `$data->get_billing_address_2();`. – Liam McArthur Apr 05 '17 at 14:01
  • Any idea if this is backward compatible with WC 2.6.x? – Bret Weinraub Apr 05 '17 at 20:03
  • 2
    @bret i read some good examples on the WooCommerce Blog e.g. https://woocommerce.wordpress.com/2017/01/17/wc-2-7-extension-compatibility-examples/ – mbg Apr 05 '17 at 21:30
20

Maybe its helpful for others too. Here's the some stuff regarding to all the functions of directly accessed values through the magic function.

This function is from Woocommerce 3.0

if ( 'completed_date' === $key ) {
        return $this->get_date_completed() ? gmdate( 'Y-m-d H:i:s', $this->get_date_completed()->getOffsetTimestamp() ) : '';
    } elseif ( 'paid_date' === $key ) {
        return $this->get_date_paid() ? gmdate( 'Y-m-d H:i:s', $this->get_date_paid()->getOffsetTimestamp() ) : '';
    } elseif ( 'modified_date' === $key ) {
        return $this->get_date_modified() ? gmdate( 'Y-m-d H:i:s', $this->get_date_modified()->getOffsetTimestamp() ) : '';
    } elseif ( 'order_date' === $key ) {
        return $this->get_date_created() ? gmdate( 'Y-m-d H:i:s', $this->get_date_created()->getOffsetTimestamp() ) : '';
    } elseif ( 'id' === $key ) {
        return $this->get_id();
    } elseif ( 'post' === $key ) {
        return get_post( $this->get_id() );
    } elseif ( 'status' === $key ) {
        return $this->get_status();
    } elseif ( 'post_status' === $key ) {
        return get_post_status( $this->get_id() );
    } elseif ( 'customer_message' === $key || 'customer_note' === $key ) {
        return $this->get_customer_note();
    } elseif ( in_array( $key, array( 'user_id', 'customer_user' ) ) ) {
        return $this->get_customer_id();
    } elseif ( 'tax_display_cart' === $key ) {
        return get_option( 'woocommerce_tax_display_cart' );
    } elseif ( 'display_totals_ex_tax' === $key ) {
        return 'excl' === get_option( 'woocommerce_tax_display_cart' );
    } elseif ( 'display_cart_ex_tax' === $key ) {
        return 'excl' === get_option( 'woocommerce_tax_display_cart' );
    } elseif ( 'cart_discount' === $key ) {
        return $this->get_total_discount();
    } elseif ( 'cart_discount_tax' === $key ) {
        return $this->get_discount_tax();
    } elseif ( 'order_tax' === $key ) {
        return $this->get_cart_tax();
    } elseif ( 'order_shipping_tax' === $key ) {
        return $this->get_shipping_tax();
    } elseif ( 'order_shipping' === $key ) {
        return $this->get_shipping_total();
    } elseif ( 'order_total' === $key ) {
        return $this->get_total();
    } elseif ( 'order_type' === $key ) {
        return $this->get_type();
    } elseif ( 'order_currency' === $key ) {
        return $this->get_currency();
    } elseif ( 'order_version' === $key ) {
        return $this->get_version();
    } elseif ( is_callable( array( $this, "get_{$key}" ) ) ) {
        return $this->{"get_{$key}"}();
    } else {
        return get_post_meta( $this->get_id(), '_' . $key, true );
    }
Jannis
  • 301
  • 2
  • 4
2

You should call the woo get function. add get_ ()

For example, change: $order->status to $order->get_status()

Elron
  • 1,235
  • 1
  • 13
  • 26