bug #14865 Fix calculating cart after trying to add more product than in stock (jakubtobiasz)

This PR was merged into the 1.12 branch.

Discussion
----------

| Q               | A                                                            |
|-----------------|--------------------------------------------------------------|
| Branch?         | 1.12
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | fixes #14723, successor of #14795
| License         | MIT



Commits
-------

4cb0a52599 Prevent the cart recalculation when the form has errors
This commit is contained in:
Grzegorz Sadowski 2023-03-27 08:23:12 +02:00 committed by GitHub
commit 7215a965ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 2 deletions

View file

@ -9,13 +9,27 @@ Feature: Verifying inventory quantity on cart summary
And the store has a product "Iron Maiden T-Shirt" priced at "12.54"
And this product is tracked by the inventory
And there are 5 units of product "Iron Maiden T-Shirt" available in the inventory
And the store has a product "Black Dress" priced at "50.20"
And this product is tracked by the inventory
And there are 10 units of product "Black Dress" available in the inventory
@ui @api
Scenario: Being unable to save a cart with product that is out of stock
Given I have added 3 products "Iron Maiden T-Shirt" in the cart
When I change product "Iron Maiden T-Shirt" quantity to 6 in my cart
And I update my cart
Then I should be notified that this product cannot be updated
Then I should be notified that this product has insufficient stock
@ui @no-api
Scenario: Preventing the cart recalculation when the form has errors
Given I have added 3 products "Iron Maiden T-Shirt" in the cart
And I have added 1 products "Black Dress" in the cart
When I change product "Iron Maiden T-Shirt" quantity to 4 in my cart
And I change product "Black Dress" quantity to 11 in my cart
Then I should be notified that this product has insufficient stock
And I should see "Iron Maiden T-Shirt" with quantity 4 in my cart
And I should see "Black Dress" with quantity 11 in my cart
And my cart's total should be "$100.36"
@ui @api
Scenario: Placing an order with products that have sufficient quantity

View file

@ -250,6 +250,7 @@ final class CartContext implements Context
/**
* @Then /^I should be notified that (this product) does not have sufficient stock$/
* @Then /^I should be notified that (this product) has insufficient stock$/
* @Then /^I should be notified that (this product) cannot be updated$/
*/
public function iShouldBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product): void

View file

@ -532,7 +532,7 @@ final class CartContext implements Context
}
/**
* @Then /^I should be notified that (this product) cannot be updated$/
* @Then /^I should be notified that (this product) has insufficient stock$/
*/
public function iShouldBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product)
{

View file

@ -112,6 +112,10 @@ class OrderController extends ResourceController
return $this->redirectHandler->redirectToResource($configuration, $resource);
}
if ($form->isSubmitted() && !$form->isValid()) {
$this->resetChangesOnCart($resource);
}
if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST));
}
@ -127,6 +131,14 @@ class OrderController extends ResourceController
);
}
private function resetChangesOnCart(OrderInterface $cart): void
{
$this->manager->refresh($cart);
foreach ($cart->getItems() as $item) {
$this->manager->refresh($item);
}
}
/**
* @psalm-suppress DeprecatedMethod
*/