0

This code:

    foreach ($order->meta_data as $row) {
        if ($row->key == 'tid') {
            $tid = $row->value;
            break;
        }
    }

within the function

function function_name($order_id, $checkout = null) {
        global $woocommerce;
        $order = new WC_Order($order_id);
        foreach ($order->meta_data as $row) {
            if ($row->key == 'tid') {
                $tid = $row->value;
                break;
            }
        }
    if (!empty($tid)) {
        ...
    }
}

add_action('woocommerce_order_status_changed', 'function_name');

triggers the PHP notice "Order properties should not be accessed directly" and I don't know what's the problem. I run Woocommerce 3.0.8 and Wordpress 4.8. Is it a bug or I do some mistake?

Update:

Fixed code looks like this:

foreach ($order->get_meta_data() as $row) {
    if ($row->key === 'tid') {
        $tid = $row->value;
        break;
    }
}

Or even better:

$tid = $order->get_meta($key = 'tid');

More info from Woocommerce docs.

3

It means you shouldn't access $order->meta_data (and other properties) directly, but use the provided getter and setter functions.

In this case, use $order->get_meta_data().

This method is (eventually) inherited from the WC_Data class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.