feat: add discount_percentage attribute to product export feed#146
Open
ahuininga-orisha wants to merge 1 commit into
Open
feat: add discount_percentage attribute to product export feed#146ahuininga-orisha wants to merge 1 commit into
ahuininga-orisha wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_percentageattribute 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
Model/Write/Products/CollectionDecorator/DiscountPercentage.phpdiscount_percentageattribute after the price stepModel/Write/Products/CollectionDecorator/Price.phpregular_price(exchange-rate-appliedprice_index.price) beforegetPriceValueoverwrites thepricekey, making it available to downstream decoratorsetc/di.xmlDiscountPercentageafterpricein both the full export (Products\Iterator) and price-only export (Price\Iterator) decorator pipelinesFormula:
discount_percentage = round((regular_price - final_price) / regular_price × 100)Known limitations (by design):
How to test
Scenario 1 — Discount percentage appears for discounted simple products
bin/magento indexer:reindex catalog_product_pricebin/magento tweakwise:export --file /tmp/test_export.xml/tmp/test_export.xmland find the product by SKU.<name><![CDATA[discount_percentage]]></name>with<value>25</value>(for 32→24 = 25%).Scenario 2 — No discount_percentage for full-price products
price_index.final_price == price_index.price.<item>block contains nodiscount_percentageattribute.Scenario 3 — Bundle products are excluded
<item>block contains nodiscount_percentageattribute, even if child products have discounts.Scenario 4 — Price-only export also includes discount_percentage
bin/magento tweakwise:export --type price --file /tmp/test_price_export.xmldiscount_percentageis present and correct.Scenario 5 — Exchange rate stores show correct percentage
discount_percentagevalue matches the expected ratio (same percentage as the base store, since both regular and final price are scaled equally).