wc_restock_refunded_items( WC_Order $order, array $refunded_line_items )

Restock items during refund.


Description Description


Parameters Parameters

$order

(Required) Order instance.

$refunded_line_items

(Required) Refunded items list.


Top ↑

Source Source

File: includes/wc-order-functions.php

function wc_restock_refunded_items( $order, $refunded_line_items ) {
	if ( ! apply_filters( 'woocommerce_can_restock_refunded_items', true, $order, $refunded_line_items ) ) {
		return;
	}

	$line_items = $order->get_items();

	foreach ( $line_items as $item_id => $item ) {
		if ( ! isset( $refunded_line_items[ $item_id ], $refunded_line_items[ $item_id ]['qty'] ) ) {
			continue;
		}
		$product            = $item->get_product();
		$item_stock_reduced = $item->get_meta( '_reduced_stock', true );
		$qty_to_refund      = $refunded_line_items[ $item_id ]['qty'];

		if ( ! $item_stock_reduced || ! $qty_to_refund || ! $product || ! $product->managing_stock() ) {
			continue;
		}

		$old_stock = $product->get_stock_quantity();
		$new_stock = wc_update_product_stock( $product, $qty_to_refund, 'increase' );

		// Update _reduced_stock meta to track changes.
		$item_stock_reduced = $item_stock_reduced - $qty_to_refund;

		if ( 0 < $item_stock_reduced ) {
			$item->update_meta_data( '_reduced_stock', $item_stock_reduced );
		} else {
			$item->delete_meta_data( '_reduced_stock' );
		}

		/* translators: 1: product ID 2: old stock level 3: new stock level */
		$order->add_order_note( sprintf( __( 'Item #%1$s stock increased from %2$s to %3$s.', 'woocommerce' ), $product->get_id(), $old_stock, $new_stock ) );

		$item->save();

		do_action( 'woocommerce_restock_refunded_item', $product->get_id(), $old_stock, $new_stock, $order, $product );
	}
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.