Jump to content

Get product id in checkout complete page


akhorasi

Recommended Posts

I am trying to find a way to get a product id in complete.tpl in whmcs.

Basically, instead of having a default button for continue to client area, I need to replace it with continue to product detail that the client has just purchased.

uri will look something like this: https://[HOSTNAME]/clientarea.php?action=productdetails&id=225

I checked the builtin hooks and I do see ShopingCartCheckoutCompletedPage which sends the variable in complete.tpl but I cant find product id details there.

This is what I tried: 

add_hook('ShoppingCartCheckoutCompletePage', 1, function($vars) {
    /**
     * select packageid from whmcs_db.tblinvoiceitems, whmcs_db.tblhosting WHERE whmcs_db.tblinvoiceitems.relid = whmcs_db.tblhosting.id and whmcs_db.tblinvoiceitems.type = 'Hosting' 
and whmcs_db.tblinvoiceitems.invoiceid = '27';.
     */

     $ordernumber = $vars['ordernumber'];
    $orderid = $vars['orderid']; 
    $invoiceid = $vars['invoiceid'];

    $productid = Capsule::table('tblinvoiceitems')
    ->join('tblhosting', 'tblinvoiceitems.relid', '=', 'tblhosting.id')
        ->where('tblinvoiceitems.invoiceid','=', $invoiceid)
        ->where('tblinvoiceitems.type', '=', 'Hosting')
    ->select('tblhosting.packageid')
        ->get();

    return $productid;
});

Above query is not working, but when I pass hardcoded value its showing properly in complete.tpl can someone please help?

Thanks

Link to comment
Share on other sites

10 hours ago, akhorasi said:

Above query is not working, but when I pass hardcoded value its showing properly in complete.tpl can someone please help?

what happens if there are two different products in the invoice ?? are you going to have multiple buttons or links ??

as written that hook should be returning an array - now it might contain 1 value or 100 values - depending on how many products there are on the invoice.

the quick fix, if you just want to get the first product value it finds in the invoice, would be to change ->get() to ->first() in the hook and that should return a specific value and not an array... and that is what is sounds as though your template is expecting to receive.

Link to comment
Share on other sites

Or use raw query 😛

Capsule::select(Capsule::raw('SELECT t1.something FROM table AS t1 LEFT JOIN table2 AS t2 ON t1.id = t2.id'));

Instead of...

$something = Capsule::table('table')
    ->join('what', 'do.you', '=', 'want.from')
        ->where('me.stupid','=', 'laravel.syntax')
    ->iHateYou()
    ->select('tblhosting.packageid')
        ->get()->first()->second()->pluck() .... really???;

 

Link to comment
Share on other sites

  • 1 year later...
On 12/06/2020 at 5:35 AM, akhorasi said:

I am trying to find a way to get a product id in complete.tpl in whmcs.

Basically, instead of having a default button for continue to client area, I need to replace it with continue to product detail that the client has just purchased.

uri will look something like this: https://[HOSTNAME]/clientarea.php?action=productdetails&id=225

I checked the builtin hooks and I do see ShopingCartCheckoutCompletedPage which sends the variable in complete.tpl but I cant find product id details there.


This is what I tried: 


add_hook('ShoppingCartCheckoutCompletePage', 1, function($vars) {
    /**
     * select packageid from whmcs_db.tblinvoiceitems, whmcs_db.tblhosting WHERE whmcs_db.tblinvoiceitems.relid = whmcs_db.tblhosting.id and whmcs_db.tblinvoiceitems.type = 'Hosting' 
and whmcs_db.tblinvoiceitems.invoiceid = '27';.
     */

     $ordernumber = $vars['ordernumber'];
    $orderid = $vars['orderid']; 
    $invoiceid = $vars['invoiceid'];

    $productid = Capsule::table('tblinvoiceitems')
    ->join('tblhosting', 'tblinvoiceitems.relid', '=', 'tblhosting.id')
        ->where('tblinvoiceitems.invoiceid','=', $invoiceid)
        ->where('tblinvoiceitems.type', '=', 'Hosting')
    ->select('tblhosting.packageid')
        ->get();

    return $productid;
});

Above query is not working, but when I pass hardcoded value its showing properly in complete.tpl can someone please help?

Thanks

Bro you have to include the below provided code at the top of your hook file

use WHMCS\Database\Capsule;

The full code block for the hook will be,

<?php
use WHMCS\Database\Capsule;
add_hook('ShoppingCartCheckoutCompletePage', 1, function($vars) {

    $ordernumber = $vars['ordernumber'];
    $orderid = $vars['orderid']; 
    $invoiceid = $vars['invoiceid'];
    
    
    $productid = Capsule::table('tblinvoiceitems')
    ->join('tblhosting', 'tblinvoiceitems.relid', '=', 'tblhosting.id')
    ->where('tblinvoiceitems.invoiceid','=', $invoiceid)
    ->where('tblinvoiceitems.type', '=', 'Hosting')
    ->select('tblhosting.packageid')
    ->get();
    
    return $productid;
});

And you can get the productid in the tpl using json_decode. An example is provided below

        {foreach $addons_html as $addon_html}
            <div class="order-confirmation-addon-output">   
               {$array = json_decode($addon_html,1)}
               {foreach $array as $key=>$item}
                    {if $key == 0}
              			<p>{$item['packageid']}</p>
                    {/if}          
               {/foreach}          
            </div>
        {/foreach}



 

Link to comment
Share on other sites

  • 2 years later...

Hey brian!,

i want product id on hook AfterShoppingCartCheckout

this is how i'm doing this

function hook_handleconnectorders_AfterShoppingCartCheckout($vars) {
    $productId = $vars['pid'] ?? null;
echo $productId;
die;
}

// Register hooks with WHMCS
add_hook('AfterShoppingCartCheckout', 1, 'hook_handleconnectorders_AfterShoppingCartCheckout');

is this the right way?

i have created the provisioning module and when user order any product save the product in database something like this

 

 

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