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() WC 4.9.1

<?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() ) {
		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;

	/*
	 * 0 as $item_quantity usually indicates we're deleting the order item.
	 * Let's restore back the reduced count.
	 */
	if ( 0 === $item_quantity ) {
		$diff = $already_reduced_stock * -1;
	}

	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();

	if ( $item_quantity > 0 ) {
		// If stock was reduced, then we need to mark this on parent order object as well so that cancel logic works properly.
		$order_data_store = WC_Data_Store::load( 'order' );
		if ( $item->get_order_id() && ! $order_data_store->get_stock_reduced( $item->get_order_id() ) ) {
			$order_data_store->set_stock_reduced( $item->get_order_id(), true );
		}
	}

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