Skip to content
  1. Extras
  2. ms3FirstTimeBuyerDiscount
  3. API and events

API and events

FtbDiscountService

Class: Ms3FirstTimeBuyerDiscount\Services\FtbDiscountService
Container key: ms3ftb_discount

Constructor

php
public function __construct(modX $modx)

Public methods

isEligible

Checks if the customer is eligible for the discount: setting is on and they have 0 paid orders.

  • For logged-in users: check by user_id
  • For guests: check by email/phone from draft -> Address
  • If draft is provided, the current order is excluded from the count
php
public function isEligible(int $userId, ?object $draft = null): bool
ParameterTypeDescription
userIdintUser ID
draftobject|nullOrder draft (optional)

Returns: true if discount can be applied, otherwise false.


getPaidOrdersCount

Number of paid orders for the user in statuses from ms3_status_for_stat.

php
public function getPaidOrdersCount(int $userId, int $excludeOrderId = 0): int
ParameterTypeDescription
userIdintUser ID
excludeOrderIdintOrder ID to exclude from count (0 = don't exclude)

Returns: number of orders (0 or more).


getGuestContactFromDraft

Gets guest contact from draft -> Address and normalizes values.

php
public function getGuestContactFromDraft(?object $draft): array
ParameterTypeDescription
draftobject|nullOrder draft

Returns: array like ['email' => string, 'phone' => string].


getPaidOrdersCountByContact

Number of paid orders for a guest by email/phone.

php
public function getPaidOrdersCountByContact(string $email, string $phone, int $excludeOrderId = 0): int
ParameterTypeDescription
emailstringGuest email
phonestringGuest phone
excludeOrderIdintOrder ID to exclude from count (0 = don't exclude)

Comparison:

  • email — lowercased
  • phone — normalized digits (tail-10 match via LIKE)

Returns: number of orders (0 or more).


calculateDiscount

Calculates cost after discount (percent or fixed).

php
public function calculateDiscount(float $cost, string $type, float $value): float
ParameterTypeDescription
costfloatOriginal amount
typestringpercent or fixed
valuefloatPercent (0–100) or fixed amount

Returns: new amount (fixed: max(0, cost - max(0, value)); percent: with 0–100 cap).


apply

Full flow: eligibility check → ftbOnBeforeApply → calculation → ftbOnApply.

php
public function apply(array $scriptProperties): ?float
KeyTypeDescription
costfloatCurrent cart cost
cartmixedMiniShop3 cart object/data
draftobject|nullOrder draft

Also respects ftb_allow_combination:

  • if false and cart already has a discount (total_discount > 0), FTB is not applied

Returns: new cost (float) or null.


MODX events

ftbOnBeforeApply

Fired before the discount is calculated. Lets you cancel application or override the base amount.

KeyTypeDescription
user_idintUser ID
costfloatCurrent cost
draftobject|nullOrder draft
cartmixedCart
settingsarrayftb_enabled, ftb_discount_type, ftb_discount_value, ftb_allow_combination

returnedValues:

KeyTypeDescription
applyboolfalse — do not apply the discount
costfloatOverride amount used for discount calculation

ftbOnApply

Fired after the discount is successfully applied.

KeyTypeDescription
user_idintUser ID
cost_beforefloatCost before discount
cost_afterfloatCost after discount
discount_amountfloatDiscount amount
draftobject|nullOrder draft

System settings

Prefix: ms3firsttimebuyerdiscount_.

KeyxtypeDefaultDescription
ftb_enabledcombo-booleantrueEnable first-time buyer discount
ftb_discount_typetextfieldpercentType: percent or fixed (case-insensitive)
ftb_discount_valuenumber10Discount value: percent (0–100) or amount
ftb_allow_combinationcombo-booleantrueAllow combining with other cart discounts

Related MiniShop3 settings:

  • ms3_status_for_stat — paid order statuses
  • ms3_status_new — added to the list of statuses as first-order marker