4

I am using WP All Import, and I use this code to do an exchange rate. I would like to do a discount randomly at the same time after converting it to my currency.

This is the code

function add_exchangerate($x) {
  $markup = 20;
  $y = $x * $markup;
  return $y;
}

What can I add to it to actually make it for example every 20th time it runs this function it will do a discount of maybe 13%?

was thinking on adding a

$i = 1;
$i++
$xnum = mt_rand(1,1000)
while ($x = $xnum) {
$z = $x/1.15
return $z
}
ingalcala
  • 1,785
  • 3
  • 14
  • 24

1 Answers1

2

Use session to store a counter,rest of the logic is pretty simple

function add_exchangerate($x)
    {
    $_SESSION['calladd'] = isset($_SESSION['calladd']) ? $_SESSION['calladd'] + 1 : 0;
    if ($_SESSION['calladd'] == 20)
        {
        $discount = 13/100;
        $_SESSION['calladd'] = 0;
        }
    else
        {
        $discount = 1;
        }

    $markup = 20;
    $y = $x * $markup;
    return $y * $discount;
    }
Mihai
  • 26,325
  • 7
  • 66
  • 81