Custom formula based on total sale


I am creating a custom rate based on a formula.
My current formula is referral_rate*product_commission. “referral_rate” and “product_commission” are custom made variables created by the following rule:

function my_variables( $variables, $rate, $context ) {
$vat_rate = 0.055;
$product_total = $variables[’t’]*$vat_rate;
// FIXME: Find sale total
$sale_total = $product_total;
$variables[’vat_rate’] = $vat_rate;
$variables[’product_total’] = $product_total;
$variables[’product_commission’] = $variables[’t’]*0.21;
if($sale_total >= 3000){
$variables[’referral_rate’] = 0.25;
}else if($sale_total >= 5000){
$variables[’referral_rate’] = 0.3;
}else if($sale_total >=7000){
$variables[’referral_rate’] = 0.4;
}else if($sale_total >=10000){
$variables[’referral_rate’] = 0.5;
$variables[’referral_rate’] = 0.2;
return $variables;
add_filter( ‘affiliates_formula_computer_variables’, ‘my_variables’, 10, 3 );

$product_commission – commission available for the affiliate
$referral_rate – rate applied based on the sale total amount vat included

In order to calculate the referral_rate I need to check the sale total amount. However since the referral are processed by product sold, I am unable to do that.
There is any information under the variable $scope or any other place that I can check for this information.

Thanks in advance,

3 Responses to Custom formula based on total sale

  1. George August 30, 2021 at 6:26 am #

    Hi Vânia,

    Yes that’s true, so if you have two products in an order the rate will be calculated for the first product and then for the second, but still the “s” variable holds the order total ( of course excluding taxes, shipping, and shipping taxes ) and is retrieved directly from the order.

    Perhaps in your case it would be more suitable to use the line total variable “t” but this is up to you to decide whether it fits for this particular case.

    Kind regards,

  2. Vânia Messiah August 27, 2021 at 3:50 pm #

    Hello George,

    Thank you for the reply.

    We are using integration with woocomerce. In your documentation is written the following “Formula rates are calculated per each product line in the order.”
    I notice that the variable “s” only contains the net value of one product of the order.
    In my previous question what I was trying to ask was if there is some variable that can retrieve the total of the order?

    Thanks Again,

  3. George August 24, 2021 at 5:39 pm #

    Hi Vania,

    Regarding formulas and the filter hook you have used, this gives you the flexibility to check even the sale amount in the order. For example the $variables[‘s’] holds the value of the net order subtotal. The variables included in the formula are four and as you have already noticed you can also create your own and define them in the $variables array.
    You should also have a look at the description found in Rates which shows the value held by each one of these variables.

    Kind regards,

We use cookies to optimize your experience on our site and assume you're OK with that if you stay.
OK, hide this message.

Affiliates · Contact · Jobs · Terms & Conditions · Privacy Policy · Documentation · Downloads · Useful Plugins · My Account