Skip to content

feat: add discount_percentage attribute to product export feed#146

Open
ahuininga-orisha wants to merge 1 commit into
developfrom
feat/211839-discount-percentage-export
Open

feat: add discount_percentage attribute to product export feed#146
ahuininga-orisha wants to merge 1 commit into
developfrom
feat/211839-discount-percentage-export

Conversation

@ahuininga-orisha

Copy link
Copy Markdown
Collaborator

Magento merchants often want to use a discount percentage in Tweakwise for faceting, sorting, or display purposes (e.g. "Show products with 20%+ discount"). Until now, there was no ready-made attribute for this — customers had to set up complex custom logic on their side to derive it from the exported price fields.

This change adds a computed discount_percentage attribute to the Tweakwise product export feed. The value is calculated from the already-available price index data (no extra queries), emitted only when a real discount exists, and skipped for bundle products where the pricing model is too complex to reliably reduce to a single percentage.

Changes

File What changed
Model/Write/Products/CollectionDecorator/DiscountPercentage.php New decorator that computes and emits the discount_percentage attribute after the price step
Model/Write/Products/CollectionDecorator/Price.php Stores regular_price (exchange-rate-applied price_index.price) before getPriceValue overwrites the price key, making it available to downstream decorators
etc/di.xml Registers DiscountPercentage after price in both the full export (Products\Iterator) and price-only export (Price\Iterator) decorator pipelines

Formula: discount_percentage = round((regular_price - final_price) / regular_price × 100)

Known limitations (by design):

  • Bundle products: skipped — pricing model too complex
  • Customer group prices: uses guest price group (pre-existing export limitation)
  • Cart/quote price rules: not reflected (Magento limitation — only catalog price rules and special prices are in the price index)

How to test

Scenario 1 — Discount percentage appears for discounted simple products

  1. In Magento Admin, set a special price (e.g. €24 on a product with regular price €32) on a simple product.
  2. Reindex: bin/magento indexer:reindex catalog_product_price
  3. Run bin/magento tweakwise:export --file /tmp/test_export.xml
  4. Open /tmp/test_export.xml and find the product by SKU.
  5. Verify it contains <name><![CDATA[discount_percentage]]></name> with <value>25</value> (for 32→24 = 25%).

Scenario 2 — No discount_percentage for full-price products

  1. Find a product with no special price and price_index.final_price == price_index.price.
  2. Run the export as above.
  3. Verify that product's <item> block contains no discount_percentage attribute.

Scenario 3 — Bundle products are excluded

  1. Find a bundle product in the feed.
  2. Verify its <item> block contains no discount_percentage attribute, even if child products have discounts.

Scenario 4 — Price-only export also includes discount_percentage

  1. Run bin/magento tweakwise:export --type price --file /tmp/test_price_export.xml
  2. Find a discounted simple product in the output.
  3. Verify discount_percentage is present and correct.

Scenario 5 — Exchange rate stores show correct percentage

  1. Configure a store view with a currency exchange rate (e.g. 2.237).
  2. Run the full export.
  3. Find the same discounted product in the store's section of the feed.
  4. Verify the discount_percentage value matches the expected ratio (same percentage as the base store, since both regular and final price are scaled equally).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant