Automattic\WooCommerce\StoreApi\Utilities

QuantityLimits::get_remaining_stockprotectedWC 1.0

Returns the remaining stock for a product if it has stock.

This also factors in draft orders.

Method of the class: QuantityLimits{}

No Hooks.

Returns

Int|float|null.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_remaining_stock( $product );
$product(WC_Product) (required)
Product instance.

QuantityLimits::get_remaining_stock() code WC 10.9.1

protected function get_remaining_stock( \WC_Product $product ) {
	if ( is_null( $product->get_stock_quantity() ) ) {
		return null;
	}

	$reserve_stock  = new ReserveStock();
	$reserved_stock = $reserve_stock->get_reserved_stock( $product, $this->get_draft_order_id() );

	return wc_stock_amount( $product->get_stock_quantity() - $reserved_stock );
}