Home » WooCommerce: Fixing Fatal error Call to undefined function wc_get_order()

WooCommerce: Fixing Fatal error Call to undefined function wc_get_order()

by Tutor Aspire

I developed a custom payment gateway plugin for a client, who wanted to add a similar method to “cod” (cash on delivery). FYI, he wanted to add a method called “card on delivery”. I simply duplicated the code, added the PHP to a file, made a plugin and gave him the plugin zip file. And everything was working great… until he did a test checkout.

The error that showed after placing an order

Fatal error: Call to undefined function wc_get_order() in /home/XXXXXXX/public_html/wp-content/plugins/woocommerce-gateway-carddelivery-plugin/woocommerce-gateway-carddelivery-plugin.php on line 201

The fix: wc_get_order() is only valid for WooCommerce 2.2+

It was very simple. The client was still using WooCommerce 2.1, where new orders are called differently. From WooCommerce 2.2, orders are created with wc_get_order() instead.

I simply substituted this:

$order = wc_get_order( $order_id );

with this:

$order = new WC_Order( $order_id );

You may also like