Fixing Amasty Affiliate Attribution Loss When Customers Use Coupon Codes
If you use Amasty Affiliate for your Magento store, there’s a bug you probably don’t know about.
Here’s the scenario. A customer clicks an affiliate link, the cookie gets set, they browse your store, add items to cart, and then apply a regular coupon code. They place the order. The affiliate gets no credit.
The affiliate did their job. They drove the customer to your store. But because the customer used a coupon code, the attribution is silently lost.
Why it happens
Inside AffiliateQuoteResolver::resolveAffiliateAccount(), Amasty uses a priority chain:
- Check if the quote has a coupon code, then look it up in the affiliate coupon table
- If the coupon is not an affiliate coupon, throw
NoSuchEntityExceptionand return null - Only if there’s no coupon at all, check the affiliate cookie
Step 2 short-circuits the entire method. When any non-affiliate coupon is present, the cookie check never runs. Affiliate rules aren’t applied during cart calculation. SalesOrderAfterPlaceObserver can’t find an affiliate account. No transaction is created. Attribution lost.
The fix
The module uses two plugins.
The first is an after plugin on resolveAffiliateAccount() that checks the affiliate cookie when the original method returns null. Simple and non-destructive.
The second handles the case where both an affiliate discount and a coupon discount apply to the same item. Rather than stacking them, the customer gets the larger of the two:
| Scenario | Result |
|---|---|
| Affiliate only | Full affiliate discount |
| Coupon only | Full coupon discount |
| Both, affiliate larger | Affiliate discount kept, coupon removed |
| Both, coupon larger | Coupon discount kept, affiliate removed |
| Both + automatic rule | max(affiliate, coupon) + auto rule applies normally |
Affiliates get credit. Margins are protected. Discounts don’t stack.
Installation
composer require pixelperfectat/magento2-module-amasty-affiliate-attribution-fix
bin/magento module:enable PixelPerfect_AmastyAffiliateAttributionFix
bin/magento setup:upgrade
No configuration needed once installed.
Worth checking
If you’re running Amasty Affiliate and use coupon codes in your store, it’s worth querying your amasty_affiliate_transaction table against your orders and seeing how many attributions are missing. The number might be uncomfortable.
The module is compatible with PHP 8.3+, Magento 2.4.x, and Amasty Affiliate 2.3.0+. GitHub repository here. If you need help with your Magento affiliate setup, get in touch.