hi david,
You helped me get this code i pasted below set up for a filter of search results. I'd like to add another filter to search by % off, say 20, 30, 40, 50% off. I've created a custom field in my database called "savings" and one called "Retprice". For items that have a retail price, i'm capturing the retail price under "retprice" and the sale price under the standard "price" field.
So given this info I'd like to do two things.
1) On import of a feed, calculated the % savings by taking the (retprice - price)/retprice * 100 ... I figured it would speed up the queries if I saved this calculation as a field rather than calculating it each time someone queries it.
2) I'd like to add code to the filter search results allow users to sort by the savings field (Percent off).
Thanks in advance.
PS -- Would it be possible to display the Retail Price, Sale price and savings on a single product? Say if savings is not null, then show Retprice, Price and savings fields?
{code saved}
Thanks David,
This is a really useful mod, but a couple of things.
Just to clarify, the file to be edited is includes/admin.php and I think there is one ) too many at the end, as it was causing an admin control panel error until I removed it.
One issue I am having is that I am getting a Division by zero error when loading some of the feeds, probably caused by an empty Retprice field, so is there a way to add and if Retprice >0 condition to the code?
Hi Ben,
Correct file - syntax corrected above (it was a missing bracket). The division by zero would only happen if retprice was zero, which should only happen if the price itself was 0.00 due to PHP string comparison of numbers. As a replacement, including bracket balancing correction; have a go with:
if (!$importRecord["retprice"] || ($importRecord["retprice"]==0.00)) $importRecord["retprice"] = $importRecord["price"];
if ($importRecord["retprice"] != "0.00")
{
$importRecord["savings"] = intval((($importRecord["retprice"] - $importRecord["price"]) / $importRecord["retprice"]) * 100 );
}
Cheers,
David.
--
PriceTapestry.com
Thanks David,
Works a treat now with no errors.
Much appreciated, as always
Ben
Hi lunen,
This should be reasonably straight forward. If there is no retprice value then it can be set to price during import and savings will calculate to zero.
To add this to the import, look for the following code around line 342:
if (!$importRecord["merchant"]) return;
...and then insert after that point the following new code:
if (!$importRecord["retprice"]) $importRecord["retprice"] = $importRecord["price"];
$importRecord["savings"] = intval((($importRecord["retprice"] - $importRecord["price"]) / $importRecord["retprice"]) * 100 );
To create the discount filter, have a go with:
print "<label form='mf'><b>By saving:</b></label><br />";
print "<select style='width:150px;' id='mf' name='savingsFilter'>";
$options = array("10","20","30","40","50");
foreach($options as $option)
{
$selected = ($_GET["savingsFilter"]==$option?"selected='selected'":"");
print "<option value='".$option."' ".$selected.">".$option."%</option>";
}
print "</select>";
print "<br /><br />";
The filter application code also needs to be added to search.php. Search the file for each position where merchantFilter is used and copy the code replacing merchantFilter with savingsFilter. However, the section where priceWhere needs to be slightly different as it needs to use a mathematical comparison rather than exact match. To do this; where you are making a copy of the merchantFilter code as follows:
if ($merchantFilter)
{
$priceWhere .= " AND merchant = '".database_safe($merchantFilter)."' ";
}
...the copy would be:
if ($savingsFilter)
{
$savingsWhere .= " AND savings >= '".database_safe($savingsFilter)."' ";
}
If you're not sure of any of the changes to search.php or html/searchresults.php email me the files and I'll add the code for you...
The new fields can easily be added to the price comparison table in html/prices.php. Add the appropriate header row for the new fields, and to avoid displaying 0% in the savings column; use something like:
<td><?php print ($product["savings"]!="0"?$product["original_name"]."%":""); ?></td>
Cheers,
David.
--
PriceTapestry.com