Support » Plugin: WooCommerce » PHP Fatal error: Uncaught Error: Call to undefined function wc_get_order()

  • Resolved jesserichard


    This is error I’m getting on version 5.1.0.

    PHP Fatal error: Uncaught Error: Call to undefined function wc_get_order()

    I suspect the WooCommerce class hasn’t been instantiated when I go to run this function, but it’s pretty straight forward (I think).

    function bought( $id ) {
        $bought = false;
        // Set HERE ine the array your specific target product IDs
        $prod_arr = array( $id );
        // // Get all customer orders
        $customer_orders = get_posts( 
                                    'posts_per_page' => -1,
                                    'meta_key'    => '_customer_user',
                                    'meta_value'  => get_current_user_id(),
                                    'post_type'   => 'shop_order', // WC orders post type
                                    'post_status' => 'wc-completed' // Only orders with status "completed"
        // // Foreach
        foreach ( $customer_orders as $customer_order ) {
            // Updated compatibility with WooCommerce 3+
            $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
            $order = wc_get_order( $customer_order );
            // Iterating through each current customer products bought in the order
            foreach ($order->get_items() as $item) {
                // WC 3+ compatibility
                if ( version_compare( WC_VERSION, '3.0', '<' ) ) 
                    $product_id = $item['product_id'];
                    $product_id = $item->get_product_id();
                // Your condition related to your 2 specific products Ids
                if ( in_array( $product_id, $prod_arr ) ) 
                    $bought = true;
        // return "true" if one the specifics products have been bought before by customer
        return $bought;

    I’m running this function from:

    add_action(‘wp_head’, ‘coldstream_head’, 1);

    So, I’m guessing it’s being called too early from wp_head, but this somewhat doesn’t make sense after playing with it forever. I’d love a second set of eyes here…

    • This topic was modified 2 months, 3 weeks ago by jesserichard.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support gabrielfuentes


    Hi there!

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    I hope that helps!

    Plugin Support gabrielfuentes


    We’ve not seen any activity on this thread for a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.


Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.