Skip to content

Duplicate one discount (return the new discount created) #40281

@tleon

Description

@tleon

Description

Add an API endpoint to duplicate an existing discount and return the newly created discount. This functionality should mirror the duplicate functionality available in the Back Office.

Required Changes:

  • Create a new API endpoint: POST /api/discounts/{discountId}/duplicate
  • The endpoint should:
    • Accept a discount ID as path parameter
    • Create a duplicate of the discount
    • Generate a new unique code if the original had a code
    • Return the complete newly created discount object
    • Handle all related data (conditions, restrictions, etc.)

Technical Details:

  • The duplicate should copy all discount properties
  • If the original discount has a code, the duplicate should have a new generated code or be code-less
  • The duplicate should be inactive by default (or follow the same pattern as product duplication)
  • All restrictions, conditions, and discount settings should be copied

How to Test

Prerequisites

  1. Set up a PrestaShop 9 instance with API access configured
  2. Create an API client with appropriate permissions
  3. Create at least one discount with various configurations:
    • Discount with code
    • Discount with customer restrictions
    • Discount with product restrictions
    • Discount with discount settings (percentage and amount)

Test Case 1: Basic Duplication

  1. Create a discount via API or Back Office:

    • Note the discount ID
    • Ensure it has various settings (name, description, discount type, etc.)
  2. Duplicate the discount:

    POST /api/discounts/{discountId}/duplicate
    Authorization: Bearer {api_token}
  3. Verify duplication:

    • Response status should be 201 Created
    • Response should include the complete newly created discount object
    • The new discount should have a different ID
    • All properties (except ID and code if applicable) should match the original
    • Verify in Back Office that both discounts exist

Test Case 2: Duplicate Discount with Code

  1. Create a discount with a code:

    POST /api/discounts
    Authorization: Bearer {api_token}
    Content-Type: application/json
    
    {
      "name": "Original Discount",
      "code": "ORIGINAL2025",
      "active": true,
      ...
    }
  2. Duplicate the discount:

    POST /api/discounts/{discountId}/duplicate
    Authorization: Bearer {api_token}
  3. Verify code handling:

    • The duplicate should either have:
      • A new generated code, OR
      • An empty code (if code-less discounts are allowed)
    • The duplicate code should be unique and not conflict with existing codes

Test Case 3: Duplicate Discount with Restrictions

  1. Create a discount with customer and product restrictions:

    • Set specific customer eligibility
    • Set product restrictions
    • Set minimum amount conditions
  2. Duplicate the discount:

    POST /api/discounts/{discountId}/duplicate
    Authorization: Bearer {api_token}
  3. Verify restrictions are copied:

    • Response should include all restrictions
    • Customer eligibility should be preserved
    • Product restrictions should be preserved
    • Minimum amount conditions should be preserved
    • Verify in Back Office that restrictions match

Test Case 4: Duplicate Non-Existent Discount

  1. Attempt to duplicate a non-existent discount:

    POST /api/discounts/99999/duplicate
    Authorization: Bearer {api_token}
  2. Verify error handling:

    • Response status should be 404 Not Found
    • Response should include an appropriate error message

Test Case 5: Verify Duplicate is Independent

  1. Create and duplicate a discount

  2. Modify the original discount:

    PUT /api/discounts/{originalId}
    Authorization: Bearer {api_token}
    Content-Type: application/json
    
    {
      "name": "Modified Original"
    }
  3. Verify independence:

    • The duplicate should remain unchanged
    • The original should have the new name
    • Both should be independent entities

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions