简体   繁体   中英

Replace outdated WC_Order_Item_Meta in WooCommerce 3

With Woocommerce, I am using a plugin to export data to an excel file but it keeps telling me that it is deprecated, would there be a way to replace those two lines to make the code work:

$product = $order_details->get_product_from_item($item);

$meta = new WC_Order_Item_Meta( $item, $product );

$meta_html = $meta->display( true, true , '_', ' | ' );

Any help is appreciated.

Since WC version 3, the WC_Order method get_product_from_item( $item ) is outdated, deprecated and has been replaced by:

$product = $item->get_product();

and $item is now an object from new WC_Order_Item_Product class, where properties can't be accessed directly, just as WC_Order or WC_Product objects too. Instead you should need to use all available methods for those classes.

Everything has changed deeply since WooCommerce version 3, regarding Orders, products and many other things. The WC_Order_Item_Meta class has been deprecated and wc_display_item_meta function is used instead.

So your code should be simply:

 $meta_html = wc_display_item_meta( $item, array( 'before' => '', 'after' => '', 
    'separator' => ' | ', 'echo' => false, 'autop' => false ) );

This should work if all other code has been updated for Woocommerce version 3+.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM