Woocommerce: Programmatically Updating Order Status

When I try to programmatically update an order during a WP_Ajax hook. IE.

$order = wc_create_order($order_data);
$order->add_product( get_product( $membership_product_ids[0] ), 1 );
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order_id = $order->id;
$order->update_status('completed');

I get the following errors:

Read More

Warning: DOMXPath::query(): Invalid expression in /wp-content/plugins/woocommerce/includes/libraries/class-emogrifier.php on line 299

Warning: DOMXPath::query(): Invalid expression in /wp-content/plugins/woocommerce/includes/libraries/class-emogrifier.php on line 299

Warning: Invalid argument supplied for foreach() in /wp-content/plugins/woocommerce/includes/libraries/class-emogrifier.php on line 302

Warning: DOMXPath::query(): Invalid expression in /wp-content/plugins/woocommerce/includes/libraries/class-emogrifier.php on line 299

Warning: DOMXPath::query(): Invalid expression in /wp-content/plugins/woocommerce/includes/libraries/class-emogrifier.php on line 299

Warning: Invalid argument supplied for foreach() in /wp-content/plugins/woocommerce/includes/libraries/class-emogrifier.php on line 302

If I remove that update_status function it does not errors.

Related posts

Leave a Reply

4 comments

  1. The errors you are seeing are coming from the notification system. Your code appears able to update the status just fine. The Emogrify Class is used to generate inline CSS in an HTML template. You are likely seeing these errors because either A) you have overridden the “Completed order” email template and have created a parsing error or B) there is a WooCommerce add-on that is conflicting in some way with what you are doing.

    To correct the issue, the first step will be to verify that it is the notification system by going into the admin under WooCommerce->Settings->Email->Completed order and un-checking the “Enable this email notification” box. Then save that and retest.

    If your error goes away, then check your theme directory for an override of that template. It would be in <themename>/woocommerce/emails/customer-completed-order.php. If you have one, them rename it so it will not override anymore and reactivate the “Completed order” notification and see if using the default template corrects the issue.

    If the issue persists with the default email notification template, then disable all of the WooCommerce add-ons you might have installed and retest. At this point, if it is working, you can re-enable them one-by-one until the issue returns. Once you have found the offending add-on, you will need to either adjust your code to allow for the add-on to not cause an issue or contact the add-on developer to address you issue.

  2. I am not sure but you could try this trick using new WC_Order( $order_id );, to avoid the error:

    // global $woocommerce; // in case of need…
    $order = wc_create_order($order_data);
    $order_id = $order->id; // moving this here
    $_order = new WC_Order( $order_id ); // here
    $_order->add_product( get_product( $membership_product_ids[0] ), 1 );
    $_order->set_address( $address, 'billing' );
    $_order->set_address( $address, 'shipping' );
    $_order->update_status('completed');
    

    I agree this is a strange issue.

  3. As it is rare issue, you’d better manually open:

    /wp-content/plugins/woocommerce/includes/libraries/class-emogrifier.php

    and before line 299, insert these lines, and see what you will get:

    var_dump($value);
    var_dump($this->translateCssToXpath($value['selector']));
    var_dump($xpath->query($this->translateCssToXpath($value['selector'])));
    
  4. I figured this out by trying a range of different hooks, the issue was that the hook I was using to create the order happened before woocommerce initialized that what caused this issue.