Jump to content

Call script ifspecial product in complete.tpl


jeroman

Recommended Posts

for 3'rd party affiliate system.

Need to have the "call script" based on product and not all products as it is today when you call it from the

complete.tpl So if I have Basic package and I have Pro package I can choose only to call the javascript/code if it's the PRO package.

 

Any idea ?

Link to comment
Share on other sites

After some tinkering, I now have:

 

 

templates/orderforms/modern/complete.tpl

<link rel="stylesheet" type="text/css" href="templates/orderforms/{$carttpl}/style.css" />

<div id="order-modern">

<h1>{$LANG.orderconfirmation}</h1>

<br />

<div class="signupfields padded">

<p>{$LANG.orderreceived}</p>

<div class="cartbox">
<p align="center"><strong>{$LANG.ordernumberis} {$ordernumber}</strong></p>
</div>

<p>{$LANG.orderfinalinstructions}</p>

{if $AffiliateProductBought}
       <img src='http://my.affiliate.link' width='0' height='0'>
{/if}

{if $invoiceid && !$ispaid}
<br />

<div class="errorbox" style="display:block;">{$LANG.ordercompletebutnotpaid}</div>
<p align="center"><a href="viewinvoice.php?id={$invoiceid}" target="_blank">{$LANG.invoicenumber}{$invoiceid}</a></p>

{/if}

{foreach from=$addons_html item=addon_html}
<div style="margin:15px 0 15px 0;">{$addon_html}</div>
{/foreach}

{if $ispaid}
<!-- Enter any HTML code which needs to be displayed once a user has completed the checkout of their order here - for example conversion tracking and affiliate tracking scripts -->
{/if}

</div>

<p align="center"><a href="clientarea.php">{$LANG.ordergotoclientarea}</a></p>

<br /><br />

</div>

 

and

 

includes/hooks/hookfile.php

<?php

/* ******************************************************
              ** Hopefully a working hook **

****************************************************** */
function hook_websitesysdouble_callcode($vars) {

<!-- Dont trust the input, make sure it's an integer -->
if (gettype($vars['invoiceid'])==="integer") {

<!-- Get the approved products from the dd -->
       $query = 'SELECT * FROM tblaffiliateprods';
       $approved_products = mysql_query($query) or die('Query failed: ' . mysql_error()); 

       $latest_invoice = $vars['invoiceid'];

<!-- Get the latest purchase from the db -->
       $query = "SELECT description FROM tblinvoiceitems WHERE invoiceid = $latest_invoice AND type = 'Hosting'";
       $latest_order_array = mysql_query($query) or die('Query failed: ' . mysql_error()); 

<!-- For every order in the latest purchase, check if it matches any of the approved products -->
       while($latest_order = mysql_fetch_array($latest_order_array)) {

               while($approved = mysql_fetch_array($approved_products)) {
                       if (strncasecmp($approved['name'], $latest_order['description'], 25) == 0) {
                               <!-- Set a variable to check for in complete.tpl -->
                               $AffiliateProductBought="true";
                       }
               }

       }

}


}

add_hook("ShoppingCartCheckoutCompletePage",1,"hook_websitesysdouble_callcode");

?>

 

The problem is... the affiliate image doesn't load.

Could anyone please help me spot where I've gone done wrong?

 

The order goes through, but nothing seems to get called.

Edited by jeroman
Added comments to the code
Link to comment
Share on other sites

For starters, you're using HTML style comments inside PHP code in your hook. You'll want to use

 

/* Multi

* Line

* Comment

*/

or

 

// One line comment

 

Second, you should use the WHMCS sql helper functions whenever possible. And theres no need to verify that invoiceid an integer. If you're concerned with that just cast it to an int instead.

 

I'd do something like this (untested):

 

<?php

function hook_websitesysdouble_callcode($vars) {
 global $smarty;
 $orderid = $vars['orderid'];
 $approved_products = array();
 $d = select_query('tblaffiliateprods', 'name', array());
 while ($approved = mysql_fetch_array($d)) {
   $approved_products[] = $approved[0];
 }

 $d = full_query("SELECT p.name FROM tblhosting h tblproducts p WHERE h.orderid = $orderid AND h.packageid = p.id LIMIT 1");
 $product = mysql_fetch_array($d);

 if (in_array($product[0], $approved_products)) {
   $AffiliateProductBought = true;
 } else {
   $AffiliateProductBought = false;
 }

 $smarty->assign('AffiliateProductBought', $AffiliateProductBought);

}


add_hook("ShoppingCartCheckoutCompletePage",999,"hook_websitesysdouble_callcode");
?>

Link to comment
Share on other sites

Thanks for helping :)

 

includes/hooks/hookfile.php now looks more like:

 

<?php

function hook_websitedouble_callcode($vars) {

//we want to check the input to avoid injection-problems
if (gettype($vars['invoiceid'])==="integer") {
 global $smarty;
 $orderid = $vars['orderid'];
 $approved_products = array();
 $d = select_query('tblaffiliateproducts', 'name', array());
 while ($approved = mysql_fetch_array($d)) {
   $approved_products[] = $approved[0];
 }

//The customer can buy several products, not all of them will be affiliate products
 $d = full_query("SELECT p.name FROM tblhosting h tblproducts p WHERE h.orderid = $orderid AND h.packageid = p.id");

 $AffiliateProductBought = false;

 while ($product = mysql_fetch_array($d)) {

   if (in_array($product[0], $approved_products)) {
     $AffiliateProductBought = true;
   }

 }

 $smarty->assign('AffiliateProductBought', $AffiliateProductBought);

}
}

add_hook("ShoppingCartCheckoutCompletePage",999,"hook_websitedouble_callcode");
?>

 

for some reason it still won't load.

How can I check if it triggers vs if it loads code off of complete.tpl?

Link to comment
Share on other sites

you can try adding var_dump($vars); to the top of the hook code. That should output all of the vars on the page.

 

Edit: also, you do not need to check the type on invoiceid. First, its returned by an internal WHMCS function, so its always going to be an integer (no chance for injection), and also, the sql helper functions will escape it to also ensure there are no injection attempts.

 

EDIT2: your (my) sql syntax is incorrect. it should be:

 

 $d = full_query("SELECT p.name FROM tblhosting h, tblproducts p WHERE h.orderid = $orderid AND h.packageid = p.id");

 

(Missing comma)

Edited by laszlof
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated