WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru
функция не описана

wc_maybe_adjust_line_item_product_stock() WC 3.6.0

Sees if line item stock has already reduced stock, and whether those values need adjusting e.g. after changing item qty.

Возвращает

true/false/Массив/WP_Error. Array of changes or error object when stock is updated (@see wc_update_product_stock). False if nothing changes.

Использование

wc_maybe_adjust_line_item_product_stock( $item, $item_quantity );
$item(WC_Order_Item) (обязательный)
Item object.
$item_quantity(числоeger)
Optional quantity to check against. Read from object if not passed.
По умолчанию: -1

Список изменений

С версии 3.6.0 Введена.

Код wc maybe adjust line item product stock: woocommerce/includes/admin/wc-admin-functions.php WC 4.3.2

<?php
function wc_maybe_adjust_line_item_product_stock( $item, $item_quantity = -1 ) {
	if ( 'line_item' !== $item->get_type() ) {
		return false;
	}

	/**
	 * Prevent adjust line item product stock.
	 *
	 * @since 3.7.1
	 * @param bool $prevent If should prevent.
	 * @param WC_Order_Item $item Item object.
	 * @param int           $item_quantity Optional quantity to check against.
	 */
	if ( apply_filters( 'woocommerce_prevent_adjust_line_item_product_stock', false, $item, $item_quantity ) ) {
		return false;
	}

	$product               = $item->get_product();
	$item_quantity         = wc_stock_amount( $item_quantity >= 0 ? $item_quantity : $item->get_quantity() );
	$already_reduced_stock = wc_stock_amount( $item->get_meta( '_reduced_stock', true ) );

	if ( ! $product || ! $product->managing_stock() || ! $already_reduced_stock || $item_quantity === $already_reduced_stock ) {
		return false;
	}

	$order                  = $item->get_order();
	$refunded_item_quantity = $order->get_qty_refunded_for_item( $item->get_id() );
	$diff                   = $item_quantity + $refunded_item_quantity - $already_reduced_stock;

	if ( $diff < 0 ) {
		$new_stock = wc_update_product_stock( $product, $diff * -1, 'increase' );
	} elseif ( $diff > 0 ) {
		$new_stock = wc_update_product_stock( $product, $diff, 'decrease' );
	} else {
		return false;
	}

	if ( is_wp_error( $new_stock ) ) {
		return $new_stock;
	}

	$item->update_meta_data( '_reduced_stock', $item_quantity + $refunded_item_quantity );
	$item->save();

	return array(
		'from' => $new_stock + $diff,
		'to'   => $new_stock,
	);
}