Jump to content
glardone

Hook to db insert

Recommended Posts

Dear all,

I'm trying to create a hook to insert the domains data in a custom table after the shopping cart checkout.

These data are:

 

- domainid

- userid

- contactid

 

and I will use them to create a custom report about the domain holders.

 

My code is:

 

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

function domains($vars) {

   $orderid = $vars['orderid'];

   $domainid = array($vars['domainid']);

   $clientid = Capsule::table('tblorders')
               ->where('id',$orderid)
               ->get('userid');

   $contacts = Capsule::table('tblorders')
               ->where('id',$orderid)
               ->get('contactid');

   mysql_query ('INSERT INTO dna_domains (domainid, userid, contactid) VALUES (?,?,?)', array( $params['domainid'], $params['userid'], $params['contactid'] ) );
}

add_hook("AfterShoppingCartCheckout", 1, "dna_domains");

 

I'm new in WHMCS and I have not understood how interact with its variables... where I'm wrong?

 

Thank you for support!

 

Best regards.

Share this post


Link to post
Share on other sites

it's also not going to help that the correct function name isn't used in the add_hook line. :roll:

 

if it were me, and this was only for a report with the required information already in existing db tables, i'd be spending time on the report query rather than copying the data to a new table to make the query simpler.

Share this post


Link to post
Share on other sites

there is few issues in your code, first of all you did not link the function actionhook point, both were defined but not linked together.

 

also it has domainids as an array, it may return one or more domain ids so you need to loop through this array and enter new record for each

 

finally the last query has incorrect syntax, you write PDO query while using regular deprecated mysql function

 

so here is the changes I have made in your code:

 

<?php 

use Illuminate\Database\Capsule\Manager as Capsule; 

add_hook("AfterShoppingCartCheckout", 1, function($vars) {

   $orderid = $vars['OrderID'];

   $order = Capsule::table('tblorders')->where('id',$orderid)->get();

   $clientid = $order[0]->userid;
   $contactid = $order[0]->contactid;

   foreach ($vars['DomainIDs'] as $domainid){

       Capsule::table('dna_domains')->insert(array("domainid" => $domainid, "userid" => $clientid, "contactid" => $contactid));
   }

});

Share this post


Link to post
Share on other sites

Dear Priestakos,

thank you for your response, I modified my hook, but it's not work properly:

 

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

function domains($vars) {

   $orderid = $vars['orderid'];

   $domainid = array($vars['domainid']);

   $clientid = Capsule::table('tblorders')
               ->where('id',$orderid)
               ->get('userid');

   $contacts = Capsule::table('tblorders')
               ->where('id',$orderid)
               ->get('contactid');

   Capsule::table('dna_domains')
   	->insert(
   		[
   			'domainid', 'userid', 'contactid' => array( $params['domainid'], $params['userid'], $params['contactid'] )
   		]
   	);
}

add_hook("AfterShoppingCartCheckout", 1, "dna_domains");

 

You have any ideas?

 

Thank you!

 

Best regards.

Share this post


Link to post
Share on other sites

if it were me, and this was only for a report with the required information already in existing db tables, i'd be spending time on the report query rather than copying the data to a new table to make the query simpler.

+1, yes it will be easier there is no unique information to use separate table for

Share this post


Link to post
Share on other sites

Dear Sentq,

thank you very much!

 

Now it is more clear how it works...

 

Best regards,

Giorgio

 

 

there is few issues in your code, first of all you did not link the function actionhook point, both were defined but not linked together.

 

also it has domainids as an array, it may return one or more domain ids so you need to loop through this array and enter new record for each

 

finally the last query has incorrect syntax, you write PDO query while using regular deprecated mysql function

 

so here is the changes I have made in your code:

 

<?php 

use Illuminate\Database\Capsule\Manager as Capsule; 

add_hook("AfterShoppingCartCheckout", 1, function($vars) {

   $orderid = $vars['OrderID'];

   $order = Capsule::table('tblorders')->where('id',$orderid)->get();

   $clientid = $order[0]->userid;
   $contactid = $order[0]->contactid;

   foreach ($vars['DomainIDs'] as $domainid){

       Capsule::table('dna_domains')->insert(array("domainid" => $domainid, "userid" => $clientid, "contactid" => $contactid));
   }

});

Share this post


Link to post
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