PHP Fatal error: Uncaught Error: Call to undefined function wc_get_order()
-
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( array( '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']; else $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…
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘PHP Fatal error: Uncaught Error: Call to undefined function wc_get_order()’ is closed to new replies.