How to use WC_Order class properly

  • I’m writing a Woocommerce plugin and I’m trying to get order information from the class WC_Order, and I get two different types of error.

    In the first case, I’m using the following code:

    public function woocommerce_loaded() {
    global $woocommerce, $post;
    $order = new WC_Order($post->ID);
    }
    And I get this:
    > PHP Fatal error: Uncaught Error: Class ‘WC_Order’ not found in C:\wamp64\www\mysite\wp-content\plugins\tutorial-plugin\tutorial-plugin.php

    In the second case, I’m using this code:

    public function woocommerce_loaded() {
    global $woocommerce, $post;
    $order = $woocommerce -> WC_Order($post->ID);
    }

    And I get the following error message:

    > PHP Fatal error: Uncaught Error: Call to a member function WC_Order() on null in C:\wamp64\www\mysite\wp-content\plugins\tutorial-plugin\tutorial-plugin.php

    And for both cases, I’m hooking my function at my __construct() method with the following code:

    add_action( ‘woocommerce_init’, array( $this, ‘woocommerce_loaded’ ) );

    I also tried using this class with my orders id that I have access in the Woocommerce Orders page, but it didn’t work either.

    I’m new in this area, I don’t know what I am doing wrong. I could use some help!

  • Hi there,

    Please contact WooCommerce support directly for help with their plugin. The forum you’re posting in now is for the hosting provider, WordPress.com, and we can only help with sites that are hosted on our servers.

    If you’re using the free version of WooCommerce you can post in their community forum, here:

    https://wordpress.org/support/plugin/woocommerce/

    Or if you’re using any premium or payment gatewy add-on downloaded from WooCommerce.com you can reach live chat and email support here:

    https://woocommerce.com/my-account/create-a-ticket/

  • The topic ‘How to use WC_Order class properly’ is closed to new replies.