• i’m using “woocommerce_payment_complete” to insert some product data in a DB.
    i need to get the id of the product that the client bought.
    i already search a lot of thing and everyone said to use this.

    "$order = wc_get_order(11975);
    $items = $order->get_items();"

    It work on template files like ‘thankyou’ etc.
    but i need to use this in function.php and i get this error.

    “Call to a member function get_order() on a non-object”

    i can’t figure out what i need to call inside the “woocommerce_payment_complete” action to solve this.

    Any hints?

    If i try use this

    $order = new WC_Order(11266);
        $items = $order->get_items();
    
        foreach ( $items as $item ) {
            // echo $item['product_id'] . "<br>";
            var_dump($item['product_id']);
        }

    the result is NULL

    wp 4.3.1
    woo 2.4.10

    https://wordpress.org/plugins/woocommerce/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    Before getting items, check if $order is an object or FALSE. Looks like it does not exist.

    Thread Starter alfaex

    (@alfaex)

    i get the error when call wc_get_order(), not even run the get_items.

    i realize that if i use wc_get_order inside the “woocommerce_payment_complete” works.
    something like that.

    add_action("woocommerce_payment_complete", "woodle_func",10);
    function woodle_func($orderId){
        $order = wc_get_order($orderId);
    }
    but i will use in some custom action.
    add_action("my_custom_action", "my_custom_func");
    function "my_custom_func($orderId){
        $order = wc_get_order($orderId);
    }

    i get the error on the $order = wc_get_order($orderId);

    thanks for the reply

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘wc_get_order on function.php gerenate error’ is closed to new replies.