Active forum topics

©2006-2017 IAAI Software

Contact Us Privacy Policy

Sort by discount (from discount or RRP in feeds)

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

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!