Skip to content

Conversation

@michalkaczmarek-bitbag
Copy link
Contributor

@michalkaczmarek-bitbag michalkaczmarek-bitbag commented Oct 9, 2025

Q A
Branch? 2.1
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Related tickets fixes #18421
License MIT

Summary by CodeRabbit

  • Bug Fixes

    • Clearing the cart now fully resets the cart and its form, removing lingering validation state and previously entered data; a fresh cart is created automatically after clearing for more reliable checkout behavior.
  • Tests

    • Added a UI scenario for clearing the cart after entering a billing address and updated UI/API test suites to include checkout address setup.

@michalkaczmarek-bitbag michalkaczmarek-bitbag requested review from a team as code owners October 9, 2025 07:47
@probot-autolabeler probot-autolabeler bot added the Shop ShopBundle related issues and PRs. label Oct 9, 2025
@coderabbitai
Copy link

coderabbitai bot commented Oct 9, 2025

Walkthrough

After clearing the cart resource, the Cart FormComponent now recreates the resource, resets the form, and clears validation state (isValidated = false, validatedFields = []) to avoid stale references and EntityNotFound errors when an address was present.

Changes

Cohort / File(s) Summary of Changes
Cart form component control-flow
src/Sylius/Bundle/ShopBundle/Twig/Component/Cart/FormComponent.php
After removing and flushing the cart resource, the method now calls createResource(), resetForm(), and resets validation state (isValidated = false, validatedFields = []) to reinitialize component state. No public API changes.
BDD feature: clearing cart
features/shop/cart/shopping_cart/clearing_cart.feature
Adds a scenario "Clearing cart after adding an address in checkout" that includes entering a billing address before clearing the cart and verifying it is empty.
Behat suite contexts (UI)
src/Sylius/Behat/Resources/config/suites/ui/cart/shopping_cart.yml
Adds sylius.behat.context.setup.checkout.address to the ui_shopping_cart suite contexts (inserted in two places) to support the new scenario.
Behat suite contexts (API)
src/Sylius/Behat/Resources/config/suites/api/cart/shopping_cart.yml
Adds sylius.behat.context.setup.checkout.address to the api_shopping_cart suite contexts to support address setup in tests.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant CartForm as Cart FormComponent
  participant Repo as Repository / EntityManager
  participant FormState as Form State
  participant Validation as Validation State

  User->>CartForm: clearCart()
  CartForm->>Repo: remove(resource)
  Repo-->>CartForm: removed
  CartForm->>Repo: flush()
  Repo-->>CartForm: flushed

  rect rgba(46,117,182,0.08)
    note right of CartForm: Reinitialization (new)
    CartForm->>CartForm: createResource()
    CartForm->>FormState: resetForm()
    CartForm->>Validation: set isValidated = false
    CartForm->>Validation: clear validatedFields
  end

  CartForm-->>User: cart cleared and reinitialized
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I nibbled crumbs from the cart at night,
Cleared the basket and made it right.
Forms reset neat, validations fall free,
No phantom address will trouble me.
Hop, hop — fresh checkout, bright as can be. 🥕🐇

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The PR title "[FIX] Fixing action clear cart after added address on shipping step" directly and specifically describes the main change in this changeset. The title accurately reflects the core bug fix implemented in FormComponent.php, where the clearCart method now reinitializes cart state to prevent EntityNotFoundException errors when an address has been set. The title is concise, clear, and provides sufficient context for teammates to understand the primary purpose of the PR without being overly broad or vague.
Linked Issues Check ✅ Passed The code changes directly address the objectives stated in issue #18421. The primary fix in FormComponent.php reinitializes the cart state by recreating the resource, resetting the form, and clearing validation state after clearing the cart, which prevents the EntityNotFoundException that occurred when an address was present. The PR includes a new test scenario that verifies the clear cart action works correctly after an address is added, and configuration changes to necessary test contexts support this scenario. The implementation comprehensively addresses the requirement to prevent the exception and ensure successful clear-cart operation with addresses set.
Out of Scope Changes Check ✅ Passed All changes in the PR are directly scoped to fixing the clear cart issue after an address is added. The FormComponent.php modification fixes the root cause, the new test scenario in clearing_cart.feature validates the fix for the specific bug scenario, and the configuration changes to both UI and API test suites add necessary context to support the testing of this fix. There are no unrelated changes, refactoring, or modifications to unrelated features present in this changeset.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8383cd4 and b0e5f0d.

📒 Files selected for processing (2)
  • features/shop/cart/shopping_cart/clearing_cart.feature (1 hunks)
  • src/Sylius/Behat/Resources/config/suites/api/cart/shopping_cart.yml (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/Sylius/Behat/Resources/config/suites/api/cart/shopping_cart.yml
  • features/shop/cart/shopping_cart/clearing_cart.feature

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

github-actions bot commented Oct 9, 2025

❌ Preview Environment deleted from Bunnyshell

Available commands:

  • 🚀 /bns:deploy to redeploy the environment

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/Sylius/Bundle/ShopBundle/Twig/Component/Cart/FormComponent.php (1)

92-107: Consider adding test coverage for this edge case.

Given that this bug involved a specific sequence (cart with address → clear cart), consider adding an integration or functional test that:

  1. Creates a cart with items
  2. Associates an address (simulating checkout progress)
  3. Calls clearCart()
  4. Verifies no exception occurs and a fresh cart is created

This would prevent regression and document the expected behavior.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 57784ee and ba7733c.

📒 Files selected for processing (1)
  • src/Sylius/Bundle/ShopBundle/Twig/Component/Cart/FormComponent.php (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{php,yaml,yml,xml,twig}

📄 CodeRabbit inference engine (AGENTS.md)

Use 4 spaces for indentation in PHP, YAML, XML, and Twig files

Files:

  • src/Sylius/Bundle/ShopBundle/Twig/Component/Cart/FormComponent.php
**/*.php

📄 CodeRabbit inference engine (AGENTS.md)

**/*.php: Use modern PHP 8.2+ syntax and features
Declare strict_types=1 in all PHP files
Follow the Sylius Coding Standard
Do not use deprecated features from PHP, Symfony, or Sylius
Use final for all classes, except entities and repositories
Use readonly for immutable services and value objects
Add type declarations for all properties, arguments, and return values
Use camelCase for variables and method names
Use SCREAMING_SNAKE_CASE for constants
Use fast returns instead of unnecessary nesting
Use trailing commas in multi-line arrays and argument lists
Order array keys alphabetically where applicable
Use PHPDoc only when necessary (e.g., @var Collection)
Group class elements in order: constants, properties, constructor, public, protected, private methods
Group getter and setter methods for the same properties together
Suffix interfaces with Interface and traits with Trait
Use use statements for all non-global classes
Sort use imports alphabetically and group by type (classes, functions, constants)

Files:

  • src/Sylius/Bundle/ShopBundle/Twig/Component/Cart/FormComponent.php
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (16)
  • GitHub Check: End-to-end tests (MariaDB) / Non-JS, PHP 8.4, Symfony ^7.3, MariaDB 11.4.7, State Machine Adapter symfony_workflow
  • GitHub Check: End-to-end tests (MariaDB) / Non-JS, PHP 8.2, Symfony ^6.4, MariaDB 10.11.13, State Machine Adapter winzou_state_machine
  • GitHub Check: End-to-end tests (MySQL) / JS with Panther, PHP 8.2, Symfony ^6.4 (test_cached), MySQL 8.0, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / JS with Panther, PHP 8.3, Symfony ^7.3 (test_cached), MySQL 8.4, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / JS with Chromedriver, PHP 8.4, Symfony ^7.3 (test_cached), MySQL 8.4, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / JS with Chromedriver, PHP 8.3, Symfony ^7.3 (test_cached), MySQL 8.4, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / JS with Panther, PHP 8.4, Symfony ^7.3 (test_cached), MySQL 8.4, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / Non-JS, PHP 8.2, Symfony ^6.4 (test_cached), MySQL 8.0, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / JS with Chromedriver, PHP 8.2, Symfony ^6.4 (test_cached), MySQL 8.0, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / Non-JS, PHP 8.4, Symfony ^7.3 (test_cached), MySQL 8.4, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / Non-JS, PHP 8.3, Symfony ^7.3 (test_cached), MySQL 8.4, Twig ^3.3
  • GitHub Check: Frontend / NodeJS 24.x
  • GitHub Check: Packages / PHP 8.3, Symfony ^7.3, ORM ^3.3
  • GitHub Check: Packages / PHP 8.2, Symfony ^6.4
  • GitHub Check: Packages / PHP 8.4, Symfony ^7.3
  • GitHub Check: End-to-end tests (PostgreSQL) / Get matrix
🔇 Additional comments (1)
src/Sylius/Bundle/ShopBundle/Twig/Component/Cart/FormComponent.php (1)

99-102: LGTM! Fix correctly addresses the EntityNotFoundException.

The addition of these lines properly resolves the issue described in #18421. By creating a fresh cart resource and resetting all associated state after removing the old cart, you prevent Doctrine from attempting to reference deleted entities (such as Address entities that were associated with the cleared cart). This ensures the component returns to a clean initial state.

The sequence is correct:

  1. Old cart removed and flushed (lines 96-97)
  2. Fresh cart resource created (line 99)
  3. Form reset to bind to new resource (line 100)
  4. Validation state cleared (lines 101-102)
  5. Form submitted with new resource (line 105)

Please verify that this fix resolves the reproduction steps from issue #18421:

  1. Add product to cart
  2. Go to checkout/address and enter address
  3. Return to cart page
  4. Click "Clear cart" — should now succeed without EntityNotFoundException

@michalkaczmarek-bitbag michalkaczmarek-bitbag force-pushed the bugfix/clear-cart-fails-on-cart-page branch from 8383cd4 to b0e5f0d Compare October 21, 2025 09:57
@Rafikooo Rafikooo merged commit a40503d into Sylius:2.1 Oct 22, 2025
48 of 49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Shop ShopBundle related issues and PRs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clear cart fails on checkout page

2 participants