You are here:  » Sort by discount (from discount or RRP in feeds)


Sort by discount (from discount or RRP in feeds)

Submitted by support on Sun, 2016-01-24 15:13 in

Hi everyone,

There are a number of threads relating separately to discount / RRP fields and a common support request is that of being able to sort by discount so I thought I would consolidate the various mods into a single solution where such information is available in feeds either by way of a literal "discount" value as a percentage, or alternatively where it needs to be calculated from an rrp (recommended retail price) field.

This is because in a multiple feed / merchant installation it is quite likely that a combination of the above exists, so with custom fields that cover both scenarios and a small modification to the import process, either method can be normalised resulting in a `discount` value being imported against which a sort can be applied.

The assumption is that in addition to a "price" field (as required anyway), a feed will optionally contain EITHER a "discount" field (as a percentage) OR an "rrp" field (in the same currency as "price").

The following dbmod.php script will add the custom fields `discount` and `rrp` using a MySQL data type of DECIMAL(10,2) which is more appropriate for both values as opposed to the suggested VARCHAR(255) for custom fields...

<?php
  
require("includes/common.php");
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."feeds`
            ADD `field_discount` VARCHAR(255) NOT NULL,
            ADD `field_rrp` VARCHAR(255) NOT NULL
            "
;
  
database_queryModify($sql,$result);
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."products`
            ADD `discount` DECIMAL(10,2) NOT NULL,
            ADD `rrp` DECIMAL(10,2) NOT NULL
            "
;
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

With the above applied, the custom fields should be added to the $config_fieldSet array in config.advanced.php as follows;

  $config_fieldSet["discount"] = "Discount";
  $config_fieldSet["rrp"] = "RRP";

With that in place, to apply the unifying calculation at import time, edit includes/admin.php and look for the following code at line 465:

    /* decimalise price */
    $importRecord["price"] = tapestry_decimalise($importRecord["price"]);

...and REPLACE with:

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

With that in place, `discount` will be populated in both cases, either where provided directly, or by calculation from RRP.

Finally, sort may be added by editing search.php and looking for the following code at line 85:

    $orderByDefault["priceDesc"] = "minPrice DESC";

...and REPLACE with:

    $orderByDefault["priceDesc"] = "minPrice DESC";
    $orderByDefault["discount"] = "discount DESC";

And then the following code at line 95:

    $orderByFullText["priceDesc"] = "minPrice DESC";

...and REPLACE with:

    $orderByFullText["priceDesc"] = "minPrice DESC";
    $orderByFullText["discount"] = "discount DESC";

And finally the following code at line 459:

      $sortPriceDesc = ($sort=="priceDesc"?"<strong>".translate("High to Low")."</strong>":"<a href='".$sortHREF."priceDesc'>".translate("High to Low")."</a>");
      $banner["h3"] = translate("Order by").": ".$sortRelevance.$sortRating." | ".translate("Price").": ".$sortPriceAsc.", ".$sortPriceDesc;

...and REPLACE with:

      $sortPriceDesc = ($sort=="priceDesc"?"<strong>".translate("High to Low")."</strong>":"<a href='".$sortHREF."priceDesc'>".translate("High to Low")."</a>");
      $sortDiscount = ($sort=="discount"?"<strong>".translate("Discount")."</strong>":"<a href='".$sortHREF."discount'>".translate("Discount")."</a>");
      $banner["h3"] = translate("Order by").": ".$sortRelevance.$sortRating.$sortDiscount." | ".translate("Price").": ".$sortPriceAsc.", ".$sortPriceDesc;

Cheers,
David
--
PriceTapestry.com

Submitted by senaite on Sat, 2016-02-06 21:19

Hi David,
I am using this Sort by discount on my latest version. Could you please help me with these:

1 - By default, I would like to have Order by Discount, not Relevance.

2 - I display percentage discount on search result pages.

<?php
 
print ($product["discount"]." ".translate("% off")); 
?>

It shows 40.00%, instead of 40%. How can I fix this?
You can see it here:
{link saved}

Thank you

Submitted by support on Mon, 2016-02-08 09:38

Hi,

To make discount the default sort, in search.php look for the following code at line 10:

  $sort = (isset($_GET["sort"])?$_GET["sort"]:"relevance");

...and REPLACE with:

  $sort = (isset($_GET["sort"])?$_GET["sort"]:"discount");

And to display as an integer value only, have a go with:

<?php
 
print (intval($product["discount"])." ".translate("% off"));
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by uck on Tue, 2017-01-10 19:46

Hi David,

I followed the above indications to sort by discount price. It worked. Then I wanted to show the discount % in the search page, so I have added

<?php
 
print (intval($product["discount"])." ".translate("% off"));
?>

in /html/searchresults.php file, but the discount doest not shows up.
Did I missed something?

Thank you

Submitted by support on Wed, 2017-01-11 08:53

Hi,

The `discount` field isn't included in the SELECT field list in the search results set re-query so you would need to add it to the SQL, so if you edit search.php and look for the following code at line 476:

      $sql2 = "SELECT id,name,normalised_name,image_url,description,price,rating FROM `".$config_databaseTablePrefix."products` WHERE id IN (".$in.")";

...and REPLACE with:

      $sql2 = "SELECT id,name,normalised_name,image_url,description,price,rating,discount FROM `".$config_databaseTablePrefix."products` WHERE id IN (".$in.")";

Cheers,
David.
--
PriceTapestry.com

Submitted by uck on Wed, 2017-01-11 11:53

Now is clear. Thanks, it works!

Submitted by bodybuildingcom... on Wed, 2017-09-20 21:16

Hi David,

Could you please tell me how I can display the discount or RRP fields in the main product pages?

Thanks.

Norbert

Submitted by support on Thu, 2017-09-21 07:42

Hi Norbert,

As the fields are per merchant they should only really be displayed within the price comparison table. Firstly, you could add new columns for each field using the logical order RRP / Price / Discount. To do this, edit html/prices.php and look for the following code at line 27:

  <th><?php print translate("Price"); ?></th>

...and REPLACE with:

  <th><?php print translate("RRP"); ?></th>
  <th><?php print translate("Price"); ?></th>
  <th><?php print translate("Discount"); ?></th>

And then the following code at line 59:

  <td class='pt_pr_price'><?php print tapestry_price($product["price"]); ?></td>

...and REPLACE with:

  <td class='pt_pr_price'><?php print tapestry_price($product["rrp"]); ?></td>
  <td class='pt_pr_price'><?php print tapestry_price($product["price"]); ?></td>
  <td class='pt_pr_price'><?php print ($product["discount"]!="0.00"?$product["discount"]."%":""); ?></td>

(the last line above uses a ternary construct to display the discount% only if not 0.00)

However a popular alternative is to use strike-through and combine the values into the price column so that you see the rrp crossed out followed by the price and below that the discount (or however you wish to arrange them), in which case just look for the following code at line 59:

  <td class='pt_pr_price'><?php print tapestry_price($product["price"]); ?></td>

...and REPLACE with:

  <?php if($product["discount"]=="0.00"): ?>
    <td class='pt_pr_price'><?php print tapestry_price($product["price"]); ?></td>
  <?php else: ?>
    <td class='pt_pr_price'>
      <s><?php print tapestry_price($product["rrp"]); ?></s>
      <br />
      <?php print tapestry_price($product["price"]); ?>
      <br />
      <?php print tapestry_translate("Save")." ".$product["discount"]; ?>%
    </td>
  <?php endif; ?>

Note: If you'd prefer to display the discount in round numbers in either of the above, in place of:

$product["discount"]

use:

intval($product["discount"])

Cheers,
David.
--
PriceTapestry.com

Submitted by bodybuildingcom... on Fri, 2017-09-22 14:30

Hi David,

Thank you for the in-depth information. It has worked as intended.

Thanks.

Norbert