Skip to content

Commit

Permalink
bug Sylius#14859 [Order] Ensure correct item in post_add event (NoR…
Browse files Browse the repository at this point in the history
…esponseMate)

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 Sylius#11491, Sylius#9407 |
| License         | MIT                                                          |


Commits
-------

814df1e [Order] Ensure correct item in `post_add` event
  • Loading branch information
GSadee authored Mar 16, 2023
2 parents 770ea40 + 814df1e commit 82cbd22
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions features/cart/shopping_cart/adding_product_to_cart.feature
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ Feature: Adding a simple product to the cart
And I should be notified that the product has been successfully added
And there should be one item in my cart
And this item should have name "T-Shirt banana"

@ui @api
Scenario: Increasing quantity of an item in cart by adding the product again
Given I have product "T-Shirt banana" in the cart
When I add this product to the cart
Then I should see "T-Shirt banana" with quantity 2 in my cart
12 changes: 10 additions & 2 deletions src/Sylius/Bundle/OrderBundle/Controller/OrderItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public function addAction(Request $request): Response
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
/** @var AddToCartCommandInterface $addToCartCommand */
$addToCartCommand = $form->getData();
[$cart, $orderItem] = [$addToCartCommand->getCart(), $addToCartCommand->getCartItem()];

$errors = $this->getCartItemErrors($addToCartCommand->getCartItem());
$errors = $this->getCartItemErrors($orderItem);
if (0 < count($errors)) {
$form = $this->getAddToCartFormWithErrors($errors, $form);

Expand All @@ -73,12 +74,14 @@ public function addAction(Request $request): Response
return $this->redirectHandler->redirectToIndex($configuration, $orderItem);
}

$this->getOrderModifier()->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem());
$this->getOrderModifier()->addToOrder($cart, $orderItem);

$cartManager = $this->getCartManager();
$cartManager->persist($cart);
$cartManager->flush();

$orderItem = $this->resolveAddedOrderItem($cart, $orderItem);

$resourceControllerEvent = $this->eventDispatcher->dispatchPostEvent(CartActions::ADD, $configuration, $orderItem);
if ($resourceControllerEvent->hasResponse()) {
return $resourceControllerEvent->getResponse();
Expand Down Expand Up @@ -242,4 +245,9 @@ protected function handleBadAjaxRequestView(RequestConfiguration $configuration,
View::create($form, Response::HTTP_BAD_REQUEST)->setData(['errors' => $form->getErrors(true, true)]),
);
}

protected function resolveAddedOrderItem(OrderInterface $order, OrderItemInterface $item): OrderItemInterface
{
return $order->getItems()->filter(fn (OrderItemInterface $orderItem): bool => $orderItem->equals($item))->first();
}
}

0 comments on commit 82cbd22

Please sign in to comment.