You are here:  » Adding Discount codes to the merchants

Support Forum



Adding Discount codes to the merchants

Submitted by kevquinlan on Sun, 2007-03-11 21:56 in

hi
i was wondering what would be involved in adding some sort of discount code that would show up if a relevant discount was available for a merchant.

so when a product was displayed for "argos" for example and i had a discount code it was displayed on the results?

Ideally if there was a page i could upload them all but failing that a database table etc that was reasonably OK to manage.

Submitted by support on Mon, 2007-03-12 08:09

Hi,

The quickest and easiest way to implement this would be to use the same mechanism as the method used to display merchant logos in this thread:

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

In this case, you would create a directory in your Price Tapestry installation called discounts.

Then, inside that directory, create individual files containing any discount codes you have with filenames matching exactly the registered merchant name.

To display a code (if one exists) on the product page for the main product look for the following code in html/product.php:

<?php if (isset($mainProduct["extraHTML"])) print $mainProduct["extraHTML"]; ?>

...and add the following new code immediately afterwards:

<?php
  $discountFilename = "discounts/".$product["merchant"];
  if (file_exists($discountFilename))
  {
    $fp = fopen($discountFilename,"r");
    $discountCode = fread($fp,128);
    fclose($fp);
    print "<p>Discount Code: ".$discountCode."</p>";
  }
?>

You could do exactly the same within the prices table my making similar modifications to html/prices.php

First, find the following code:

<th width='200' align='left'><?php print translate("Stockist"); ?></th>

...and add the following code to display the new column header:

<th align='left'><?php print translate("Discount Code"); ?></th>

And then after the following line:

<td><a href='<?php print $product["merchantHREF"]; ?>'><?php print $product["merchant"]; ?></a></td>

...add the following code:

<?php
  $discountFilename = "discounts/".$product["merchant"];
  if (file_exists($discountFilename))
  {
    $fp = fopen($discountFilename,"r");
    $discountCode = fread($fp,128);
    fclose($fp);
    print "<td>".$discountCode."</td>";
  }
  else
  {
    print "<td>&nbsp;</td>";
  }
?>

Finally, you might want to display codes on the search results, but you would only be able to do this when there was only 1 merchant for the product. To do this, in html/searchresults.php find the following code:

<p><?php print substr($product["description"],0,250); ?></p>

...add the following code on the next line:

<?php
  if ($product["numMerchants"] == 1)
  {
    $discountFilename = "discounts/".$product["merchant"];
    if (file_exists($discountFilename))
    {
      $fp = fopen($discountFilename,"r");
      $discountCode = fread($fp,128);
      fclose($fp);
      print "<p>Discount Code: ".$discountCode."</p>";
    }
?>

Hope this helps!
Cheers
David,

Submitted by think on Fri, 2007-06-01 17:45

Hi David,

I'm sure I'm missing something, but I'm getting an error on products.php

The error is :

Review This Product
Warning: fopen(discounts/) [function.fopen]: failed to open stream: Permission denied in C:\XXX\html\product.php on line 40
Warning: fread(): supplied argument is not a valid stream resource in C:\XXX\html\product.php on line 41
Warning: fclose(): supplied argument is not a valid stream resource in C:\XXX\html\product.php on line 42
Discount Code:

And the code used is :

<?php if (isset($mainProduct["extraHTML"])) print $mainProduct["extraHTML"]; ?>
  <!-- Start Discount Code -->
  <?php
  $discountFilename = "discounts/".$product["merchant"];
  if (file_exists($discountFilename))
  {
$fp = fopen($discountFilename,"r");
$discountCode = fread($fp,128);
fclose($fp);
print "<p>Discount Code: ".$discountCode."</p>";
  }
?>
<!-- End Discount Code -->

I've added the code to all the pages you mention, but this is the only one I get an error on.

Also, as a side note, what should the files within the 'discounts' directory be? txt, php? And what happens when you have more than one code?

Thanks in advance,

Danny

Submitted by support on Fri, 2007-06-01 18:00

Hi Danny,

It's saying "Permission Denied", so in other words PHP cannot open a file in the discounts/ directory. This might have happened if you created the directory as a different user to the one that PHP is running as.

You should be able to fix this using your FTP program. In the remote window, when browsing your Price Tapestry installation directory, see if there is a "Properties" or "Permissions" option for the "discounts" folder. You might be able to right-click on the folder name, and then see the properties or permissions option.

Once you've found the permissions, the easiest thing to do is make the folder "World Readable", - basically just select or check every option under the "Read" column. This should allow PHP to access the directory and stop the error message...

The files in the discounts/ directory should be plain HTML as they are included directly into the page. The file extension doesn't really matter - .txt is fine.

Hope this helps,
Cheers,
David.

Submitted by Kelly and Stuart on Wed, 2008-07-09 12:32

Hi David
I have used this code on the product.php file and it doesn't bring up the discount file contents, yet it works on prices.php
Example page
any ideas?
cheers
kelly

Submitted by support on Wed, 2008-07-09 12:36

Hi Kelly,

Where within html/prices.php the code references the variable:

$product["merchant"]

...within the majority of html/product.php you would need to use:

$mainProduct["merchant"]

That should be all it is...

Cheers,
David.

Submitted by Kelly and Stuart on Wed, 2008-07-09 13:27

Brill, many thanks

Submitted by Kelly and Stuart on Thu, 2008-07-10 00:38

Is it possible to order the products.php differently.

I still want the order to be by price, but where merchants are the same price, can the order be the discount coded one first, or even alphabetical, because in this example mower warehouse comes before mow direct.

I tried a couple of things, but I just ended up putting errors all over the place (whoops!)

Many thanks
K

Submitted by support on Thu, 2008-07-10 12:23

Hi Kelly,

This should be possible using the usort() function to sort the $prices["products"] array by price, then by whether or not a discount code is present. Try adding the following code to the top of html/prices.php:

<?php
  
function sortPrices($a,$b)
  {
    if (
$a["price"] == $b["price"]) {
      
$discountFilename "discounts/".$a["merchant"];
      if (
file_exists($discountFilename))
      {
        return -
1;
      }
      else
      {
        return 
0;
      }
    }
    return (
$a["price"] < $b["price"]) ? -1;
  }
  
usort($prices["products"],"sortPrices");
?>

Cheers,
David.

Submitted by Kelly and Stuart on Fri, 2008-07-11 00:51

Hi David (sorry to be a pain!)

It works for the prices.php bit fine, but on the product.php mower warehouse is still coming up before mow direct and not displaying the code in that section of the page.

is this easy to do?
K

Submitted by support on Fri, 2008-07-11 09:49

Hi Kelly,

Just as the code to display the discount voucher needed to be slightly different in prices.php as compared to product.php; so to does the sort code. Try using the following at the top of html/product.php:

<?php
  function sortPricesProduct($a,$b)
  {
    if ($a["price"] == $b["price"]) {
      $discountFilename = "discounts/".$a["merchant"];
      if (file_exists($discountFilename))
      {
        return -1;
      }
      else
      {
        return 0;
      }
    }
    return ($a["price"] < $b["price"]) ? -1 : 1;
  }
  usort($product["products"],"sortPricesProduct");
?>

If it still doesn't display the code, let me know and i'll take a look at as that may be something a bit different...

Cheers,
David.

Submitted by Kelly and Stuart on Tue, 2008-07-15 14:40

That's the ticket!
Many thanks for your help.
K

Submitted by paddyman on Sat, 2008-07-19 16:03

Hi David,

Have implented this discount codes mod and it works great :) A lot of the merchants who I promote are generally giving out product related codes rather than codes for all mercahant products. Would it be possible to have a more advanced version of this mod which would allow me to create product related codes and if no product related codes were available the merchant codes would be shown.

I have all my products "product mapped" so all have the same title. Could I create a product subfolder under dicounts/merchant for each product which has a specific code.

Something like

$discountFilename = "discounts/".$product["merchant"]/.$product["name"];

Not too sure if this is possible. Any ideas would be much appreciated :)

Adrian

Submitted by support on Mon, 2008-07-21 08:36

Hi Adrian,

Something along those lines would certainly work. As the file_exists() function does not distinguish between a normal file and a directory name, it would be necessary to have a subtly different name for the merchant directory (for example ending with an underscore), so you would have the filename:

merchant_/product

Then, to look for either a merchant product code (first priority) or a merchant code (second priority) for the main product, instead of:

<?php
  
if ($product["numMerchants"] == 1)
  {
    
$discountFilename "discounts/".$product["merchant"];
    if (
file_exists($discountFilename))
    {
      
$fp fopen($discountFilename,"r");
      
$discountCode fread($fp,128);
      
fclose($fp);
      print 
"<p>Discount Code: ".$discountCode."</p>";
    }
?>

use something like:

<?php
  
if ($product["numMerchants"] == 1)
  {
    
$productDiscountFilename "discounts/".$product["merchant"]."_/".$product["name"];
    
$merchantDiscountFilename "discounts/".$product["merchant"];
    if (
file_exists($productDiscountFilename))
    {
      
$discountFilename $productDiscountFilename;
    }
    elseif (
file_exists($merchantDiscountFilename))
    {
      
$discountFilename $merchantDiscountFilename;
    }
    if (
$discountFilename)
    {
      
$fp fopen($discountFilename,"r");
      
$discountCode fread($fp,128);
      
fclose($fp);
      print 
"<p>Discount Code: ".$discountCode."</p>";
    }
  }
?>

Cheers,
David.

Submitted by paddyman on Mon, 2008-07-21 22:27

Hi David

Thanks for that. Trying to implement it in the prices table and have modified code but is not showing the dicounts column in the prices table.

<?php
  
if ($product["numMerchants"] == 1)
  {
    
$productDiscountFilename "discounts/".$product["merchant"]."_/".$product["name"];
    
$merchantDiscountFilename "discounts/".$product["merchant"];
    if (
file_exists($productDiscountFilename))
    {
      
$discountFilename $productDiscountFilename;
    }
    elseif (
file_exists($merchantDiscountFilename))
    {
      
$discountFilename $merchantDiscountFilename;
    }
    if (
$discountFilename)
    {
      
$fp fopen($discountFilename,"r");
      
$discountCode fread($fp,128);
      
fclose($fp);
      print 
"<td>".$discountCode."</td>";
  }
   else
  {
    print 
"<td>&nbsp;</td>";
  }
  }
?>

Can you see whats wrong? I can send you on the link if that helps.

Thanks

Adrian

Submitted by support on Tue, 2008-07-22 08:43

Hi Adrian,

My apologies; within the product page (i.e. html/prices.php; numMerchants is not a part of the product record - that's part of the search results. In this case, go straight into the file_exists code, in other words:

<?php
  $discountFilename 
"";
  
$productDiscountFilename "discounts/".$product["merchant"]."_/".$product["name"];
  
$merchantDiscountFilename "discounts/".$product["merchant"];
  if (
file_exists($productDiscountFilename))
  {
    
$discountFilename $productDiscountFilename;
  }
  elseif (
file_exists($merchantDiscountFilename))
  {
    
$discountFilename $merchantDiscountFilename;
  }
  if (
$discountFilename)
  {
    
$fp fopen($discountFilename,"r");
    
$discountCode fread($fp,128);
    
fclose($fp);
    print 
"<td>".$discountCode."</td>";
  }
    else
  {
    print 
"<td>&nbsp;</td>";
  }
?>

I've also added an additional line at the top to reset the code in each iteration of the prices loop; otherwise as soon as a discount code was found it would be displayed for every product afterwards!

Hope this helps,

Cheers,
David.

Submitted by paddyman on Tue, 2008-07-22 13:30

Thanks David,

Works great :)

Adrian

Submitted by Kelly and Stuart on Thu, 2008-07-24 08:36

Hi David

I have just come across a page where the code hasn't worked. Mower Warehouse is above mowdirect. Any ideas?

Following on from the last question about product specific codes; Mow Direct's coupon code is only applicable to product over £300 excluding mountfields. Is this something that can be added to the products applicable, or will it affect all codes?

Submitted by support on Thu, 2008-07-24 11:59

Hi Kelly

So that I can see all the code in context, could you email me products.php, html/product.php and html/prices.php and i'll check it out for you...

Cheers,
David.

Submitted by Kelly and Stuart on Sat, 2008-07-26 16:33

Yay Brill thanks David.

Submitted by npaitken on Thu, 2009-07-23 13:29

Hi David,

I've just implemented your code above into my html/prices.php

However, rather than create a new column just for discount code info, I've added your code so that the discount code info appears underneath the product description in the prices table. See code below:

<td><a href='<?php print tapestry_buyURL($product); ?>' title='<?php print "Visit Store"?>' <?php print javascript_statusBar("Go to ".$product["merchant"]); ?>><?php print $product["name"]; ?></a><br/>
<?php
  $discountFilename = "discounts/".$product["merchant"];
  if (file_exists($discountFilename))
  {
    $fp = fopen($discountFilename,"r");
    $discountCode = fread($fp,128);
    fclose($fp);
    print "$discountCode.";
  }
  else
  {
    print "&nbsp;";
  }
?>
</td>

It's working fine apart from one thing: the discount code info is being truncated! Is that because of some limit on the number of characters for the description?

Submitted by support on Thu, 2009-07-23 13:31

Hi Neil,

It's probably because of this line:

    $discountCode = fread($fp,128);

Change the 128 to something much larger the largest description you're expected - something like 1024 would probably do the trick...

Cheers,
David.

Submitted by npaitken on Thu, 2009-07-23 14:17

Thanks David, that did the trick!

Submitted by npaitken on Fri, 2009-07-31 14:47

Hi David,

Just realised that discount code info isn't displaying in pricesExternal.php. Any idea what needs to be changed?

Thanks,
Neil

Submitted by support on Fri, 2009-07-31 14:57

Hi Neil,

I think that's because of the path in the following line:

  $discountFilename = "discounts/".$product["merchant"];

...not being correct when being called via pricesExternal.php.

Try changing that line as follows:

  $discountFilename = $common_path."discounts/".$product["merchant"];

...as $common_path is set by the calling code on your external page and would otherwise be empty if not it should work fine in both scenarios.

Cheers,
David.