Merge pull request #13329 from woocommerce/fix-13320

Delete cached object when updating stock
This commit is contained in:
Claudio Sanches 2017-02-23 18:20:40 -03:00 committed by GitHub
commit e0cffc0934
2 changed files with 22 additions and 0 deletions

View File

@ -35,6 +35,7 @@ function wc_update_product_stock( $product, $stock_quantity = null, $operation =
delete_transient( 'wc_low_stock_count' );
delete_transient( 'wc_outofstock_count' );
delete_transient( 'wc_product_children_' . ( $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id() ) );
wp_cache_delete( 'product-' . $product_id_with_stock, 'products' );
// Re-read product data after updating stock, then have stock status calculated and saved.
$product_with_stock = wc_get_product( $product_id_with_stock );

View File

@ -145,6 +145,27 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
WC_Helper_Product::delete_product( $product->get_id() );
}
public function test_wc_update_product_stock_increase_decrease() {
$product = WC_Helper_Product::create_simple_product();
update_post_meta( $product->get_id(), '_manage_stock', 'yes' );
wc_update_product_stock( $product->get_id(), 5 );
$new_value = wc_update_product_stock( $product->get_id(), 1, 'increase' );
$product = new WC_Product_Simple( $product->get_id() );
$this->assertEquals( 6, $product->get_stock_quantity() );
$this->assertEquals( 6, $new_value );
$new_value = wc_update_product_stock( $product->get_id(), 1, 'decrease' );
$product = new WC_Product_Simple( $product->get_id() );
$this->assertEquals( 5, $product->get_stock_quantity() );
$this->assertEquals( 5, $new_value );
WC_Helper_Product::delete_product( $product->get_id() );
}
/**
* Test wc_delete_product_transients().
*