You are here:  » Apply Voucher Code Before Discount Calculation


Apply Voucher Code Before Discount Calculation

Submitted by ChrisNBC on Fri, 2017-09-22 14:09 in

Hi David,

I wondered if you might be able to help me resolve an issue with calculated savings displayed on my site. I’ve recently started using voucher codes and notice the GBP savings value displayed on my product page does not take account of any live voucher codes.

The voucher codes admin setting is:

$config_useVoucherCodes = 2;

I checked the raw feed and the PT products table and the voucher codes are being applied correctly. I ***think*** what might be happening is the savings value is being calculated prior to the voucher code being applied. The discount calculation code in admin.php is currently:

  /*calculate percentage discount and populate discount field */
  if (($importRecord["normalprice"] != "0.00") && ($importRecord["normalprice"] != $importRecord["price"]))
  {
    $importRecord["discount"] = (($importRecord["normalprice"]-($importRecord["price"])) / ($importRecord["normalprice"])*100);
    $importRecord["discount"] = tapestry_decimalise($importRecord["discount"]);
  }
  /*calculate actual discount and populate discountGBP field */
   if (($importRecord["normalprice"] != "0.00") && ($importRecord["normalprice"] != $importRecord["price"]))
  {
    $importRecord["discountgbp"] = (($importRecord["normalprice"]-($importRecord["price"])));
    $importRecord["discountgbp"] = tapestry_decimalise($importRecord["discountgbp"]);
  }
    /* construct search_name value */
    $searchName = tapestry_search($normalisedName);
    if (!$importRecord["merchant"]) return;
    if ($config_useVoucherCodes == 2)
    {
      $importRecordArray = tapestry_applyVoucherCodes(array($importRecord));
      $importRecord = $importRecordArray[0];
    }

Could you possibly suggest a way to resolve this? (assuming my diagnosis of the issue is correct).

Thanks in advance.

Best regards
Chris

Submitted by support on Fri, 2017-09-22 15:21

Hi Chris,

That would make sense - the sections can be swapped around so the voucher codes are applied before your custom discount mods - have a go with;

    if ($config_useVoucherCodes == 2)
    {
      $importRecordArray = tapestry_applyVoucherCodes(array($importRecord));
      $importRecord = $importRecordArray[0];
    }
  /*calculate percentage discount and populate discount field */
  if (($importRecord["normalprice"] != "0.00") && ($importRecord["normalprice"] != $importRecord["price"]))
  {
    $importRecord["discount"] = (($importRecord["normalprice"]-($importRecord["price"])) / ($importRecord["normalprice"])*100);
    $importRecord["discount"] = tapestry_decimalise($importRecord["discount"]);
  }
  /*calculate actual discount and populate discountGBP field */
   if (($importRecord["normalprice"] != "0.00") && ($importRecord["normalprice"] != $importRecord["price"]))
  {
    $importRecord["discountgbp"] = (($importRecord["normalprice"]-($importRecord["price"])));
    $importRecord["discountgbp"] = tapestry_decimalise($importRecord["discountgbp"]);
  }
    /* construct search_name value */
    $searchName = tapestry_search($normalisedName);
    if (!$importRecord["merchant"]) return;

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2017-09-22 21:35

Hi David,

Thanks for the code which has fixed the issue. In retrospect, I should have just tried reversing the sections of code but I didn't think it would be that simple!..

Thanks again.

Best regards
Chris