PHPackages                             pixelperfectat/magento2-module-amasty-affiliate-attribution-fix - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. pixelperfectat/magento2-module-amasty-affiliate-attribution-fix

ActiveMagento2-module

pixelperfectat/magento2-module-amasty-affiliate-attribution-fix
===============================================================

Fixes Amasty Affiliate attribution loss when non-affiliate coupon codes are applied

016PHP

Since Feb 10Pushed 3mo agoCompare

[ Source](https://github.com/pixelperfectat/magento2-module-amasty-affiliate-attribution-fix)[ Packagist](https://packagist.org/packages/pixelperfectat/magento2-module-amasty-affiliate-attribution-fix)[ RSS](/packages/pixelperfectat-magento2-module-amasty-affiliate-attribution-fix/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

PixelPerfect\_AmastyAffiliateAttributionFix
===========================================

[](#pixelperfect_amastyaffiliateattributionfix)

Fixes affiliate attribution loss in Amasty Affiliate when a non-affiliate coupon code is applied alongside an affiliate cookie.

The Problem
-----------

[](#the-problem)

When a customer arrives via an affiliate link, Amasty Affiliate sets a cookie (`current_affiliate_account_code`). During cart calculation, `AffiliateQuoteResolver::resolveAffiliateAccount()` uses this cookie to identify the affiliate.

However, the method uses a priority chain with a bug:

1. Check `quote->getCouponCode()` — if found, look it up in the affiliate coupon table
2. If the coupon is **not** an affiliate coupon → `NoSuchEntityException` → **return null immediately**
3. Only if no coupon code is present → read the cookie

When a customer enters a regular (non-affiliate) coupon, step 2 short-circuits and the affiliate cookie is never checked. This causes:

- Affiliate rules are no longer applied during cart calculation
- `SalesOrderAfterPlaceObserver` cannot find an affiliate account → no transaction → **attribution lost**

The Fix
-------

[](#the-fix)

### 1. Cookie Fallback (AffiliateQuoteResolverPlugin)

[](#1-cookie-fallback-affiliatequoteresolverplugin)

An **after plugin** on `AffiliateQuoteResolver::resolveAffiliateAccount()` that falls back to the cookie when the original method returns null but the cookie is present.

### 2. Max-of-Two Discount (DiscountMaxComparisonPlugin)

[](#2-max-of-two-discount-discountmaxcomparisonplugin)

An **after plugin** on `Magento\SalesRule\Model\Quote\Discount::collect()` that prevents affiliate and coupon discounts from stacking. When both apply to the same item, only the **larger** discount is kept.

- Coupon-specific rules (requiring a coupon code) are compared against affiliate rules
- Non-coupon rules (auto/site-wide sales) are **not** affected — they always apply
- Per-item comparison using Magento's built-in per-rule discount breakdown

Installation
------------

[](#installation)

```
composer require pixelperfectat/magento2-module-amasty-affiliate-attribution-fix
bin/magento module:enable PixelPerfect_AmastyAffiliateAttributionFix
bin/magento setup:upgrade
```

Configuration
-------------

[](#configuration)

None required — the module works automatically once installed.

Discount Stacking Behaviour
---------------------------

[](#discount-stacking-behaviour)

ScenarioResultAffiliate onlyFull affiliate discount appliedCoupon onlyFull coupon discount appliedBoth, affiliate largerAffiliate discount applied, coupon removedBoth, coupon largerCoupon discount applied, affiliate removedBoth + auto ruleMax(affiliate, coupon) + auto ruleTo control stacking per-rule via Magento's built-in mechanism, set `stop_rules_processing` on individual sales rules.

Compatibility
-------------

[](#compatibility)

- PHP 8.3+
- Magento 2.4.x
- Amasty Affiliate 2.3.0+

Testing
-------

[](#testing)

```
vendor/bin/phpunit
```

### Manual Verification

[](#manual-verification)

1. Click an affiliate link → verify cookie is set
2. Add items to cart
3. Apply a regular (non-affiliate) coupon code
4. Place order
5. Check `amasty_affiliate_transaction` table → affiliate transaction exists
6. Check Admin → Affiliate → Transactions → order appears with correct affiliate

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance55

Moderate activity, may be stable

Popularity8

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/9df00e858fe20fc93f406cf4ec649ed6a2c91aac6f021047b2ec273ad465e3b7?d=identicon)[pixelhed](/maintainers/pixelhed)

---

Top Contributors

[![pixelhed](https://avatars.githubusercontent.com/u/1169770?v=4)](https://github.com/pixelhed "pixelhed (6 commits)")

### Embed Badge

![Health badge](/badges/pixelperfectat-magento2-module-amasty-affiliate-attribution-fix/health.svg)

```
[![Health](https://phpackages.com/badges/pixelperfectat-magento2-module-amasty-affiliate-attribution-fix/health.svg)](https://phpackages.com/packages/pixelperfectat-magento2-module-amasty-affiliate-attribution-fix)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
