get_product_from_item($item) deprecated replacement in WooCommerce

admin

Administrator
Staff member
I'm trying to fix some deprecated functions from this <a href="https://wordpress.org/plugins/sagepay-direct-gateway-for-woocommerce/" rel="nofollow noreferrer">unsupported SagePay plugin</a>.

How can I replace the following snippet of deprecated code in WooCommerce?

Code:
foreach ($order-&gt;get_items() as $item) {
if ($item['qty']) {
  $product = $order-&gt;get_product_from_item($item);

I can see that this is its replacement:

<blockquote>
@deprecated Add deprecation notices in future release. Replaced with
Code:
$item-&gt;get_product()
</blockquote>

But simply changing it to
Code:
$product = $item-&gt;get_product();
doesn't work. I've also tried changing that line to:

Code:
$product = wc_get_product($item['id']);

But it causes an internal server error during the checkout.