不应直接访问订单属性 - WooCommerce 3.0

时间:2017-04-05 12:48:23

标签: php wordpress woocommerce

我刚刚将我当地的WooCommerce网站升级到3.0。一切都像平常一样完美,但我注意到调试开启时我收到了以下数百条通知:

[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

因此看起来WooCommerce正在退回能够直接调用订单数据。这个代码被触发的一个例子就是我的functions.php文件中的这个函数:

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

此功能只是将“EDN”添加到订单ID的开头并填充10个字符,但WooCommerce不喜欢我如何调用$order - 这是重写这样的最佳方式3.0很满意的功能?

3 个答案:

答案 0 :(得分:18)

它说“id被称为错误。不应直接访问订单属性。”

尝试$order->get_id()

答案 1 :(得分:13)

也许它对其他人也有帮助。这里有关于通过魔术函数直接访问值的所有函数的一些内容。

此功能来自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 );
    }

答案 2 :(得分:0)

您应该调用 woo get 函数。添加get_ ()

例如更改: $order->status$order->get_status()