Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mholubowski/53cd4592766bfa1a05a09e56dbdc5709 to your computer and use it in GitHub Desktop.
Save mholubowski/53cd4592766bfa1a05a09e56dbdc5709 to your computer and use it in GitHub Desktop.
solid_affiliate_custom_commission_calculator
/**
*
* Goal of this custom snippet:
*
* Override all commission calculations with a fixed value.
* The value should be the value of \SolidAffiliate\Lib\Settings::KEY_REFERRAL_RATE
* Ensure that only affiliate referred orders receive a commission.
* Make sure to catch any errors
*
* Add a note to the Settings > General page to inform the user of this change.
*
* Use this filter:
*
* Filters the total commission amount for an order before returning from the commission calculation.
*
* This allows for custom modifications to the commission amount by plugins or themes
* before the final amount is set. The filter passes the calculated total commission,
* the affiliate object, and the order description object to allow for detailed adjustments
* based on the order or affiliate details.
*
* @param float $total The calculated total commission for the order.
* @param \SolidAffiliate\Models\Affiliate $affiliate The affiliate object for whom the commission is calculated.
* @param \SolidAffiliate\Lib\VO\OrderDescription $order_description The order description object containing details about the order.
* @return float The modified total commission amount.
* $total = (float)apply_filters("solid_affiliate/commission_calculator/commission_for_order", $total, $affiliate, $order_description);
*/
function solid_affiliate_custom_commission_calculator(float $total, \SolidAffiliate\Models\Affiliate $affiliate, \SolidAffiliate\Lib\VO\OrderDescription $order_description): float {
try {
// Get the referral rate from the settings
$referral_rate = \SolidAffiliate\Lib\Settings::get(\SolidAffiliate\Lib\Settings::KEY_REFERRAL_RATE);
$referral_rate_type = \SolidAffiliate\Lib\Settings::get(\SolidAffiliate\Lib\Settings::KEY_REFERRAL_RATE_TYPE);
if ($referral_rate_type === 'flat' && $referral_rate > 0) {
$total = $referral_rate;
}
// Return the possibly adjusted commission total
return $total;
} catch (\Exception $e) {
error_log('Error occurred while applying custom commission calculation: ' . $e->getMessage());
return $total;
}
}
add_action('init', function () {
add_filter('solid_affiliate/commission_calculator/commission_for_order', 'solid_affiliate_custom_commission_calculator', 10, 3);
});
add_action('admin_init', function () {
add_action('solid_affiliate/settings/group_heading/after', function (string $settings_group_name) {
if ($settings_group_name === 'Referral Rate') {
echo '<style>
.solid-affiliate-custom-commission-calculator-settings-note {
display: block;
border: 2px dashed gold;
padding: 20px;
background: #ffd70082;
width: 80%;
line-height: 120%;
}
</style>
';
echo "<div class='solid-affiliate-custom-commission-calculator-settings-note'>
Pour le type 'Flat', la valeur saisie dans le champs 'Taux de commissions' détermine la commission totale par commande. Pour le type 'Pourcentage', la commission est calculée par article sans taux spécifique par commande.
</div>
";
};
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment