PrestaShop icon indicating copy to clipboard operation
PrestaShop copied to clipboard

Add form in BO to apply discount automatically or via promo code

Open boherm opened this issue 6 months ago • 2 comments

Questions Answers
Branch? develop
Description? Add form in BO to apply discount automatically or via promo code
Type? improvement
Category? BO
BC breaks? no
Deprecations? no
How to test? See #38654
UI Tests https://github.com/boherm/ga.tests.ui.pr/actions/runs/15606833727
Fixed issue or discussion? Fixes #38654
Related PRs ~
Sponsor company PrestaShop SA

Summary by CodeRabbit

  • New Features

    • Introduced a new "Usability conditions" section in the discount creation and editing forms, allowing selection between automatic discounts and code-based discounts.
    • Added the ability to generate and validate unique discount codes directly in the admin interface, with real-time feedback for duplicate codes.
  • Bug Fixes

    • Improved error messages for discount code uniqueness violations, now displaying the conflicting discount ID.
  • Tests

    • Added integration and unit tests to ensure discount code uniqueness and correct form behavior.
  • Documentation

    • Updated usage instructions for new discount usability features in the admin interface.

boherm avatar Jun 12 '25 09:06 boherm

This pull request seems to contain new translation strings. I have summarized them below to ease up review:

  • Admin.Catalog.Feature
    • [ ] Usability conditions
    • [ ] Create automatic discount
    • [ ] Generate discount code
    • [ ] Specifiy discount mode

(Note: this is an automated message, but answering it will reach a real human)

ps-jarvis avatar Jun 12 '25 09:06 ps-jarvis

Walkthrough

This update introduces the ability to create discounts that are either automatically applied or require a unique promo code. It adds new form types, validation logic for code uniqueness, UI toggling for usability modes, and comprehensive tests for discount creation and update scenarios involving promo codes.

Changes

File(s) Change Summary
admin-dev/themes/new-theme/js/components/generatable-input.ts Added constructor for selector injection; updated documentation.
admin-dev/themes/new-theme/js/pages/discount/discount-event-map.ts Added new event map module with discountUsabilityModeChange event.
admin-dev/themes/new-theme/js/pages/discount/discount-map.ts Added selectors for code generator input and usability mode select input.
admin-dev/themes/new-theme/js/pages/discount/form/index.ts Initialized toggler for discount code input visibility and event handling for usability mode.
src/Adapter/Discount/Repository/DiscountRepository.php Added method to get discount ID by code; set repository in validator.
src/Adapter/Discount/Validate/DiscountValidator.php Introduced repository dependency and setter; updated uniqueness check logic and messages.
src/Core/ConstraintValidator/Constraints/UniqueDiscountCode.php Added new constraint class for unique discount code validation.
src/Core/ConstraintValidator/UniqueDiscountCodeValidator.php Added validator class for unique discount code using repository lookup.
src/Core/Form/IdentifiableObject/DataHandler/DiscountFormDataHandler.php Changed logic to set discount code from form data for create and update.
src/Core/Form/IdentifiableObject/DataProvider/DiscountFormDataProvider.php Extended form data to include usability mode and code.
src/PrestaShopBundle/Form/Admin/Sell/Discount/DiscountType.php Added 'usability' field of type DiscountUsabilityType to the discount form.
src/PrestaShopBundle/Form/Admin/Sell/Discount/DiscountUsabilityType.php Introduced form type for usability mode and code, with conditional validation and UI logic.
src/PrestaShopBundle/Resources/config/services/core/constraint_validator.yml Registered the new unique discount code validator as a service.
tests/Integration/Behaviour/Features/Context/Domain/Discount/DiscountFeatureContext.php Added assertion for discount code uniqueness violation.
tests/Integration/Behaviour/Features/Scenario/Discount/BO/add_discount_with_code.feature Added feature scenarios for discount creation and update with code uniqueness checks.
tests/Unit/Core/ConstraintValidator/UniqueDiscountCodeValidatorTest.php Added unit tests for unique discount code validator covering multiple scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant AdminUser
    participant DiscountForm
    participant DiscountUsabilityType
    participant UniqueDiscountCodeValidator
    participant DiscountRepository

    AdminUser->>DiscountForm: Submit form (create/update discount)
    DiscountForm->>DiscountUsabilityType: Build form fields
    DiscountForm->>UniqueDiscountCodeValidator: Validate code (if code mode)
    UniqueDiscountCodeValidator->>DiscountRepository: getIdByCode(code)
    DiscountRepository-->>UniqueDiscountCodeValidator: Return discount ID or null
    UniqueDiscountCodeValidator-->>DiscountForm: Pass or fail validation
    DiscountForm-->>AdminUser: Show success or error

Assessment against linked issues

Objective Addressed Explanation
Allow creation of discounts that are automatically applied or via promo code (#38654)
Allow user to define a unique promo code for discounts; enforce uniqueness (#38654)
Ensure promo code uniqueness validation with appropriate error message on conflict (#38654)
Changing the promo code does not affect the application of the related discount (#38654)
Use input UI similar to referenced InformationType for promo code entry (#38654)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Poem

A rabbit hopped through fields of code, Where discounts bloom and savings flowed. Now codes are checked, unique and bright, No duplicates escape the light! Forms now toggle, tests abound, In PrestaShop, good deals are found. 🐇✨

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Jun 13 '25 13:06 coderabbitai[bot]

Hello @boherm,

Thank you for your PR,

I tested your PR and it's good for me. Capture d’écran 2025-07-08 à 11 05 38

Capture d’écran 2025-07-08 à 11 05 53

Thank you ^^

SiraDIOP avatar Jul 08 '25 09:07 SiraDIOP

QA OK without required approvals !? :trollface: