Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Drop stock reservation when removing item from cart via the Store API (
Browse files Browse the repository at this point in the history
…#3468)

* Remove Blocks version of ReserveStock Class

* When a cart item is removed, remove holds on stock

* Move maybe_release_stock to abstract

* Update ReserveStockException usage
  • Loading branch information
mikejolley authored Dec 2, 2020
1 parent a0c27bf commit e05daaa
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 309 deletions.
15 changes: 15 additions & 0 deletions src/StoreApi/Routes/AbstractCartRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ protected function maybe_recalculate_totals() {
}
}

/**
* If there is a draft order, releases stock.
*
* @return void
*/
protected function maybe_release_stock() {
$draft_order = wc()->session->get( 'store_api_draft_order', 0 );

if ( ! $draft_order ) {
return;
}

wc_release_stock_for_order( $draft_order );
}

/**
* Get route response when something went wrong.
*
Expand Down
1 change: 1 addition & 0 deletions src/StoreApi/Routes/CartRemoveItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ protected function get_route_post_response( \WP_REST_Request $request ) {
}

$cart->remove_cart_item( $request['key'] );
$this->maybe_release_stock();

return rest_ensure_response( $this->schema->get_item_response( $cart ) );
}
Expand Down
6 changes: 3 additions & 3 deletions src/StoreApi/Routes/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use Automattic\WooCommerce\Blocks\Domain\Services\CreateAccount;
use Automattic\WooCommerce\Blocks\StoreApi\Utilities\CartController;
use Automattic\WooCommerce\Blocks\StoreApi\Utilities\OrderController;
use Automattic\WooCommerce\Blocks\StoreApi\Utilities\ReserveStock;
use Automattic\WooCommerce\Blocks\StoreApi\Utilities\ReserveStockException;
use Automattic\WooCommerce\Checkout\Helpers\ReserveStock;
use Automattic\WooCommerce\Checkout\Helpers\ReserveStockException;
use Automattic\WooCommerce\Blocks\Payments\PaymentResult;
use Automattic\WooCommerce\Blocks\Payments\PaymentContext;

Expand Down Expand Up @@ -332,7 +332,7 @@ private function is_valid_draft_order( $order_object ) {
private function create_or_update_draft_order() {
$cart_controller = new CartController();
$order_controller = new OrderController();
$reserve_stock = \class_exists( '\Automattic\WooCommerce\Checkout\Helpers\ReserveStock' ) ? new \Automattic\WooCommerce\Checkout\Helpers\ReserveStock() : new ReserveStock();
$reserve_stock = new ReserveStock();
$this->order = $this->get_draft_order_id() ? wc_get_order( $this->get_draft_order_id() ) : null;

// Validate items etc are allowed in the order before it gets created.
Expand Down
13 changes: 4 additions & 9 deletions src/StoreApi/Schemas/CartItemSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Automattic\WooCommerce\Blocks\StoreApi\Schemas;

use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi;

use Automattic\WooCommerce\Checkout\Helpers\ReserveStock;

/**
* CartItemSchema class.
Expand Down Expand Up @@ -346,15 +346,10 @@ protected function get_remaining_stock( \WC_Product $product ) {
return null;
}

$draft_order = wc()->session->get( 'store_api_draft_order', 0 );

if ( \class_exists( '\Automattic\WooCommerce\Checkout\Helpers\ReserveStock' ) ) {
$reserve_stock = new \Automattic\WooCommerce\Checkout\Helpers\ReserveStock();
} else {
$reserve_stock = new \Automattic\WooCommerce\Blocks\StoreApi\Utilities\ReserveStock();
}

$draft_order = wc()->session->get( 'store_api_draft_order', 0 );
$reserve_stock = new ReserveStock();
$reserved_stock = $reserve_stock->get_reserved_stock( $product, $draft_order );

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

Expand Down
12 changes: 4 additions & 8 deletions src/StoreApi/Utilities/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Automattic\WooCommerce\Blocks\StoreApi\Routes\RouteException;
use Automattic\WooCommerce\Blocks\StoreApi\Utilities\NoticeHandler;
use Automattic\WooCommerce\Checkout\Helpers\ReserveStock;

/**
* Woo Cart Controller class.
Expand Down Expand Up @@ -630,14 +631,9 @@ protected function get_product_quantity_in_cart( $product ) {
* @return int
*/
protected function get_remaining_stock_for_product( $product ) {
if ( \class_exists( '\Automattic\WooCommerce\Checkout\Helpers\ReserveStock' ) ) {
$reserve_stock_controller = new \Automattic\WooCommerce\Checkout\Helpers\ReserveStock();
} else {
$reserve_stock_controller = new \Automattic\WooCommerce\Blocks\StoreApi\Utilities\ReserveStock();
}

$draft_order = wc()->session->get( 'store_api_draft_order', 0 );
$qty_reserved = $reserve_stock_controller->get_reserved_stock( $product, $draft_order );
$reserve_stock = new ReserveStock();
$draft_order = wc()->session->get( 'store_api_draft_order', 0 );
$qty_reserved = $reserve_stock->get_reserved_stock( $product, $draft_order );

return $product->get_stock_quantity() - $qty_reserved;
}
Expand Down
230 changes: 0 additions & 230 deletions src/StoreApi/Utilities/ReserveStock.php

This file was deleted.

57 changes: 0 additions & 57 deletions src/StoreApi/Utilities/ReserveStockException.php

This file was deleted.

Loading

0 comments on commit e05daaa

Please sign in to comment.