Support » Plugin: WooCommerce » “Order properties should not be accessed directly

  • I’m having some issues when trying to access the line-items of an order.

    The e-mails being sent out fails to get the line items when I run the following code:

    public function maybe_do_cool_stuff( $order_id ){
    	
    	$order = new WC_Order( $order_id );
    						
        	$items = $order->get_items();	
    }
    
    add_action( 'woocommerce_new_order', array( My_Class::get_instance(),'maybe_do_cool_stuff' ), 10, 1 );

    If I comment out $order->get_items(), then all the line items are sent as usual in the customer / admin emails. When the line is there, no order-lines are showing in any emails.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mike Jolley

    (@mikejolley)

    First off, use wc_get_order( $order_id ), and ensure it’s not false before continuing.

    using get_items() is fine. it’s your usage of those items which would throw notices.

    Thread Starter svax

    (@svax)

    Hey,

    thanks for your quick reply.

    My code currently:

    
    if( !$order = wc_get_order( $order_id ) ) return false;
    			
    $items = $order->get_items();	

    No change unfortunately. No line-items are showing up in the emails being sent out. removing the get_items() line fixes it 🙁

    Here is the complete error message, only changed the base referer:

    payment_status was called incorrectly. Order properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), do_action('template_redirect'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, WC_AJAX::do_wc_ajax, do_action('wc_ajax_checkout'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, WC_AJAX::checkout, WC_Checkout->process_checkout, WC_Checkout->process_order_payment, WC_Gateway_Invoice->process_payment, WC_Order->update_status, WC_Order->save, WC_Order->status_transition, do_action('woocommerce_order_status_completed'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, WC_Emails::send_transactional_email, do_action_ref_array, WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, WC_Email_Customer_Completed_Order->trigger, WC_Email->get_content, WC_Email_Customer_Completed_Order->get_content_html, wc_get_template_html, wc_get_template, include('/plugins/woocommerce/templates/emails/customer-completed-order.php'), do_action('woocommerce_email_order_details'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, WC_Emails->order_details, wc_get_template, include('/plugins/woocommerce/templates/emails/email-order-details.php'), do_action('woocommerce_email_before_order_table'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, WC_Gateway_Invoice->email_instructions, WC_Abstract_Legacy_Order->__get, wc_doing_it_wrong. This message was added in version 3.0., referer: http://my-domain.com/checkout/

    Plugin Author Mike Jolley

    (@mikejolley)

    Look in WC_Gateway_Invoice (plugin?). Look for a call to ->payment_method and replace with ->get_payment_method()

    Thread Starter svax

    (@svax)

    Hms…yeah found that and replaced. Seems there is also a referance to $order->payment_status which I’m guessing messes up aswell. I couldn’t find any get_payment_status() method… are there any other alternatives to this?

    The code from the plugin in mention is:

    public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
        if ( $this->instructions && ! $sent_to_admin && 'invoice' === $order->get_payment_method() && apply_filters( 'wc_invoice_gateway_process_payment_order_status', $this->order_status ) !== $order->payment_status ) {
          echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
        }
      }
    Thread Starter svax

    (@svax)

    Ok this is weird… removed the entire function which generated the error messages. But still no line-items are being shown in the admin and customer emails while I run this code on the woocommerce_new_order hook:

    if( !$order = wc_get_order( $order_id ) ) return false;
    
    $order_items = $order->get_items();			
    			 

    There are currently no error messages in the logs. This is what the order emails look like:

    Email confirmation

    Same result if I try running $order->get_data(); instead of $order->get_items();

    • This reply was modified 4 years, 2 months ago by svax.
    Thread Starter svax

    (@svax)

    Update:

    This problem only occurs if I run the $order->get_items() ( or $order->get_data() ), in the woocommerce_new_order hook.

    If I change my function to run on the hook “woocommerce_thankyou”, all works well.

    Plugin Author Mike Jolley

    (@mikejolley)

    woocommerce_new_order may be too early for orders items during checkout I guess. Checkout creates the order, then adds items.. something must be triggering a save event before it completes.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘“Order properties should not be accessed directly’ is closed to new replies.