Change item discount to Buy X Get One Y Percent Off (v. 2.8x)

If you turn on the item discounts:

Storeadmin -> Catalog Setup -> Price Discount = Yes

Your client will be able to set quantity discounts on a per-item level. These discounts affect all quantities. For instance, if a client adds an item with a price of $10.00, and sets a discount of "2 or more = 50% off", the system will give a 50% discount to all items if the customer orders two or more. (e.g. 1 = $10.00, 2 = $5.00 ea, 3 = $5.00 ea, and so on)

If you'd like to change this functionality to a BOGO type discount, where a customer gets a certain discount off for every X items ordered, you can use the following modification.

Important! This modification will completely replace the current discount functionality with a 'buy X get the next Y% off' functionality, where only the first discount entry will be used to determine the discount. Note that only up to a quantity of 99 will be given a discount. (e.g. If a customer orders 100 widgets, the 100th and beyond will not be discounted.)
  1. Open the items.php file in the /storeadmin/ folder.

  2. Change the following code (around line 635):

    for ($i = 1; $i <= 5; ++$i)

    to

    for ($i = 1; $i <= 1; ++$i)
  3. Change the following code (around line 647):

    echo "";
    echo "";
    echo " or more ";
    echo "";
    echo "- ";
    echo " ";
    echo "% off  ";
    echo "$currency off";
    echo "";
    echo "";

    to

    echo "";
    echo "";
    echo "Every ";
    echo "";
    echo " - get ";
    echo " ";
    echo "% off";
    echo "";
    echo "";
  4. Open the order.php file in the /go/ folder.

  5. Find the following code (around line 382):

    if ($discountpr)
    {
    $discpr = "";
    $discinfo = explode("~", $discountpr);
  6. Just under this line, change the code:

    for ($dc=0; $dc <= count($discinfo); ++$dc)
    {
    $discval = explode(",", $discinfo[$dc]);
    if ($dc == 0)
    {
    $dprqty = $discval[0]-1;
    $newprice = $price;
    }
    else
    {
    $olddc = $dc-1;
    $olddiscval = explode(",", $discinfo[$olddc]);
    if ($dc == count($discinfo))
    $dprqty = 0;
    else
    $dprqty = $discval[0]-$olddiscval[0];
    if ($olddiscval[2] == "A")
    $newprice = $price - $olddiscval[1];
    else
    $newprice = $price * (100-$olddiscval[1]) / 100;
    }
    // If Wholesale
    if ($wsdisc > 0)
    $newprice = $newprice*(100-$wsdisc)/100;
    // If Member
    else if ($memdisc > 0)
    $newprice = $newprice*(100-$memdisc)/100;
    if ($dc > 0)
    $discpr .= ":";
    if (empty($newprice))
    $discpr .= "$dprqty,0.00";
    else
    $discpr .= "$dprqty," .number_format((float)$newprice, 2, '.', '');
    }

    to

    // BUY X GET NEXT AT Y% OFF START
    $bogosplit=explode(",", $discinfo[0]);
    $everyx=$bogosplit[0];
    $bogodisc=$bogosplit[1];
    // FIRST ENTRY IS FULL PRICE
    $discpr="1,$price";
    $ongoingprice=$price;
    for($vx=2; $vx<=99; ++$vx)
    {
    if($vx%$everyx==0 AND $bogodisc<=100)
    $newprice=$price*((100-$bogodisc)/100);
    else
    $newprice=$price;
    $ongoingprice=$ongoingprice+$newprice;
    $new_price=$ongoingprice/$vx;
    $discpr.=":1,".number_format($new_price, 2, '.', '');
    }
    $discpr.=":0,$price";
    // BUY X GET NEXT AT Y% OFF END
  7. Open the products_variables.php file in the /enc/ folder.

  8. Find the code (around line 10)

    $label_discmsg2 = " or more and receive ";

    and change it to:

    $label_discmsg2 = ", get ";
  9. Find the code (around line 181):

    for ($dc=0; $dc < count($discountinfo); ++$dc)
    {
    $discinfo = explode(",", $discountinfo[$dc]);
    if ($dc > 0)
    $dispdiscounts .= "
    "; $dispdiscounts .= $label_discmsg1 .$discinfo[0] .$label_discmsg2; if ($discinfo[2] == "A") $dispdiscounts .= $Currency .number_format((float)$discinfo[1], 2, '.', '') ." off"; else $dispdiscounts .= "a $discinfo[1]% discount"; } $dispdiscounts .= ""; if ($membprice == "yes" AND (empty($membcode) OR $membdisc == 0)) $dispdiscounts = "";

    and change it to:

    $discinfo = explode(",", $discountinfo[0]);
    if(count($discountinfo)==1 AND $discinfo[2]=='D' AND $discinfo[1]>0 AND $discinfo[1]<=100 AND $discinfo[0]>1)
    {
    $prevqty=$discinfo[0]-1;
    $dispdiscounts .= $label_discmsg1 .$prevqty .$label_discmsg2;
    if($discinfo[1]==100)
    $dispdiscounts .= "the next one free";
    else
    $dispdiscounts .= "$discinfo[1]% off the next one";
    $dispdiscounts .= "";
    if ($membprice == "yes" AND (empty($membcode) OR $membdisc == 0))
    $dispdiscounts = "";
    }
  10. Save and reload all files.

  11. In the storeadmin area, select the item to receive the discount, and add the quantity to receive the discount, along with the discount percent.

    ex.)

    Buy 1, Get 1 50% off

    Enter:

    Every 2 - get 50% off

    ex.)

    Buy 2, get the 3rd 50% off

    Enter:

    Every 3 - get 50% off

    ex.)

    Buy 3, get the 4th for free

    Enter:

    Every 4 - get 100% off