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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

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