wc_update_product_stock()WC 3.0.0

Update a product's stock amount.

Uses queries rather than update_post_meta so we can do this in one query (to avoid stock issues).

Возвращает

true|false|int|null.

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

wc_update_product_stock( $product, $stock_quantity, $operation, $updating );
$product(int|WC_Product) (обязательный)
Product ID or product instance.
$stock_quantity(int|null)
Stock quantity.
По умолчанию: null
$operation(строка)
Type of operation, allows 'set', 'increase' and 'decrease'.
По умолчанию: 'set'
$updating(true|false)
If true, the product object won't be saved here as it will be updated later.
По умолчанию: false

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

С версии 3.0.0 Введена.
С версии 3.0.0 this supports set, increase and decrease.

Код wc_update_product_stock() WC 9.3.3

function wc_update_product_stock( $product, $stock_quantity = null, $operation = 'set', $updating = false ) {
	if ( ! is_a( $product, 'WC_Product' ) ) {
		$product = wc_get_product( $product );
	}

	if ( ! $product ) {
		return false;
	}

	if ( ! is_null( $stock_quantity ) && $product->managing_stock() ) {
		// Some products (variations) can have their stock managed by their parent. Get the correct object to be updated here.
		$product_id_with_stock = $product->get_stock_managed_by_id();
		$product_with_stock    = $product_id_with_stock !== $product->get_id() ? wc_get_product( $product_id_with_stock ) : $product;
		$data_store            = WC_Data_Store::load( 'product' );

		// Fire actions to let 3rd parties know the stock is about to be changed.
		if ( $product_with_stock->is_type( 'variation' ) ) {
			// phpcs:disable WooCommerce.Commenting.CommentHooks.MissingSinceComment
			/** This action is documented in includes/data-stores/class-wc-product-data-store-cpt.php */
			do_action( 'woocommerce_variation_before_set_stock', $product_with_stock );
		} else {
			// phpcs:disable WooCommerce.Commenting.CommentHooks.MissingSinceComment
			/** This action is documented in includes/data-stores/class-wc-product-data-store-cpt.php */
			do_action( 'woocommerce_product_before_set_stock', $product_with_stock );
		}

		// Update the database.
		$new_stock = $data_store->update_product_stock( $product_id_with_stock, $stock_quantity, $operation );

		// Update the product object.
		$data_store->read_stock_quantity( $product_with_stock, $new_stock );

		// If this is not being called during an update routine, save the product so stock status etc is in sync, and caches are cleared.
		if ( ! $updating ) {
			$product_with_stock->save();
		}

		// Fire actions to let 3rd parties know the stock changed.
		if ( $product_with_stock->is_type( 'variation' ) ) {
			// phpcs:disable WooCommerce.Commenting.CommentHooks.MissingSinceComment
			/** This action is documented in includes/data-stores/class-wc-product-data-store-cpt.php */
			do_action( 'woocommerce_variation_set_stock', $product_with_stock );
		} else {
			// phpcs:disable WooCommerce.Commenting.CommentHooks.MissingSinceComment
			/** This action is documented in includes/data-stores/class-wc-product-data-store-cpt.php */
			do_action( 'woocommerce_product_set_stock', $product_with_stock );
		}

		return $product_with_stock->get_stock_quantity();
	}
	return $product->get_stock_quantity();
}