3

on WooCommerce, I'm trying to get the variation name of a product. But I'm getting the message error : Call to undefined function wc_get_product(). Here is the code: (the file where this code is, is from a application that make some request to the server, so it isn't on the root directory).

$order_items = $order_data->line_items;
foreach ($order_items as $order_item) {
    if ($order_item->product_id == $product_id) {
        // Get variation if any
        $variation_id = $order_item->variation_id;
        $variation_name = wc_get_product($variation_id);
        $variation_name->get_formatted_name();

        if (empty($variation_id)) $variation_id = 0;

        $variation_prop = $product['variations'][$variation_id];
    }

}

Any help is appreciated.

EDITED 2: The $order_data is got this way:

try {
                $woocommerce = new Client($f3->get('wprestbaseurl'), $f3->get('wprestconsumer_key'), $f3->get('wpconsumer_secret'), ['wp_api' => true, 'version' => 'wc/v3', 'query_string_auth' => (0 === strpos(strtolower($f3->get('wprestbaseurl')), 'https://'))]);
                // Retrieve given order meta data
                // System IDs are stored into the "xlspadlock_activations" custom field for each order.
                $order_data = $woocommerce->get("orders/$customer_order_id");
            }
catch{....
TiagoSantos
  • 127
  • 8
  • I totally understand @LoicTheAztec that it's not quite the best way to help. But since the code is not mine, I have some restritions on posting. But I kindly ask for your help! I edited the main post to EDIT 2:, where I show how $order_data is got. – TiagoSantos Nov 06 '20 at 16:38
  • As it seems that you are using some Rest API on an external file, WooCommerce Classes and methods can't work and will throw errors if you try to use them. – LoicTheAztec Nov 06 '20 at 20:43
  • I can't call them diretly from "wp-content\plugins\woocommerce\includes", using some url path? Do you see an alternative? ;) – TiagoSantos Nov 06 '20 at 21:15
  • I am not enough skilled regarding REST API, so for the moment I don't have any clue for you. – LoicTheAztec Nov 06 '20 at 21:27
  • I understand this is a specific question. Thank you for your time! I'll wait for someone to help. – TiagoSantos Nov 09 '20 at 10:48

1 Answers1

1

Update 3

After your update and comments, As it seems that you are using some Rest API on an external file, WooCommerce Classes and methods can't work and will throw errors if you try to use them.


Initial answer:

Your code has some mistakes since WooCommerce 3, as order items and product properties can't not be accessed directly.

Use instead WC_Order_Item_Product get product_id(), get_variation_id() and get_product() methods as follows:

$order_items = $order->get_items(); // Get order items

// Loop through order items
foreach ( $order_items as $item) {
    // Check for specific product Id and only for product variation items
    if ( in_array( $product_id, array($item->get product_id(), $item->get variation_id() ) ) && $item->get variation_id() > 0 ) {
        // Get product variation object
        $product_variation = $item->get_product();

        // Get the product variation formatted name
        $formatted_name   = $product_variation->get_formatted_name();
        
        // ...
    }
}

Related threads:

Notes: On order items, when an item is a product variation, the method get product_id() is the parent variable product Id and the method get_product() gives the product variation instance object.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks Loic, I edited my main post, adding the first line of code. The variation_id is working well, but the varition_name isn't. I tried your solution but I keep getting this error : [link] (https://ibb.co/99XB9fk) . Basically it's for any function I use, I guess.. – TiagoSantos Nov 05 '20 at 11:59
  • @TiagoSantos The $order object need to be defined to avoid this error… As you are using a special API on an external file normal woocommerce way doesn't seem to work... – LoicTheAztec Nov 09 '20 at 13:23