You are here:  » Discount percentage


Discount percentage

Submitted by marco@flapper on Wed, 2016-09-21 14:23 in

Hi,
For using with pt plugin I created an optional field "Discount" and a field "stprijs". In "stprijs" I import the old/standard price (when available), but I'm looking now for a way to populate "Discount".

I found this thread but I'm not sure where to put the code:

http://www.pricetapestry.com/node/5568

Submitted by support on Wed, 2016-09-21 15:01

Hi Marco,

Edit includes/admin.php and look for the following code at line 466:

    $importRecord["price"] = tapestry_decimalise($importRecord["price"]);

...and REPLACE with:

    /* decimalise price */
    $importRecord["price"] = tapestry_decimalise($importRecord["price"]);
    $importRecord["stprijs"] = tapestry_decimalise($importRecord["stprijs"]);
    if (($importRecord["stprijs"] != "0.00") && ($importRecord["stprijs"] != $importRecord["price"]))
    {
      $importRecord["discount"] = (($importRecord["stprijs"]-($importRecord["price"])) / ($importRecord["stprijs"])*100);
      $importRecord["discount"] = tapestry_decimalise($importRecord["discount"]);
    }

The above would calculate discount to 2 decimal places. If you'd prefer a round number then in place of:

      $importRecord["discount"] = tapestry_decimalise($importRecord["discount"]);

...use:

      $importRecord["discount"] = intval(tapestry_decimalise($importRecord["discount"]));

You can then display the discount within any PriceTapestry.org for WordPress template that supports the %DB_fieldname% placeholder (see Translation and HTML Modification). If it is possible that there is no discount, it is straight forward to add conditionality based on whether a custom field is populated or not. If you need to do this, for example for swapping out within the Price Comparison table (Prices / Each template) edit the plugin file pto_product.php and look for the following code beginning at line 152:

      foreach($matches[1] as $field)
      {
        $each = str_replace("%DB_".$field."%",$product->$field,$each);
      }

...and REPLACE with:

      foreach($matches[1] as $field)
      {
        if ($product->$field)
        {
          $each = str_replace('%IFDB_'.$field.'%','',$each);
          $each = str_replace('%ENDIFDB_'.$field.'%','',$each);
        }
        else
        {
          $each = preg_replace('/%IFDB_'.$field.'%(.*)%ENDIFDB_'.$field.'%/U','',$each);
        }
        $each = str_replace("%DB_".$field."%",$product->$field,$each);
      }

And then you could use, for example:

%IFDB_discount%
Save %DB_discount%%
%ENDIFDB_discount%

Cheers,
David.
--
PriceTapestry.com