From 0cd5c1da6615e9bfee5202e494d082c7c88b50f4 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 26 Apr 2019 17:24:11 +0100 Subject: [PATCH] Allow stock to be force read without a change --- .../data-stores/class-wc-product-data-store-cpt.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index 585464e387b..ed66458a9d0 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -363,6 +363,19 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da $product->set_props( $set_props ); } + /** + * Re-reads stock from the DB ignoring changes. + * + * @param WC_Product $product Product object. + * @param int|float $new_stock New stock level if already read. + */ + public function read_stock_quantity( &$product, $new_stock = null ) { + $object_read = $product->get_object_read(); + $product->set_object_read( false ); + $product->set_stock_quantity( is_null( $new_stock ) ? get_post_meta( $product->get_id(), '_stock', true ) : $new_stock ); + $product->set_object_read( $object_read ); + } + /** * Read extra data associated with the product, like button text or product URL for external products. *