Jump to content

Email Notification on Order Cancelled/Terminated


GoogieHost

Recommended Posts

Personally for such an item, I would think opening a ticket would be better but tickets don't do templates.   Would be possible to do by grabbing a template from the db and tossing it in to smarty with the order variables.  

If there is enough demand I will do the template part later on. 

 

<?php
/** @author steven99 **/
add_hook('CancelOrder', 1, function($vars) {

    if (isset($vars['orderid']) and is_numeric($vars['orderid'])) // should be there, but defensive coding mode active
    {
        $results = localAPI('GetOrders', array('id' => $vars['orderid']));
        if ($results and isset($results['result']) AND $results['result'] =='success' and isset($result['order']['order'][0]]))
        {
            $Order = $result['order']['order'][0]; // supplied one id, get oen order
            //got the cancelled order, now what?
            
            //open ticket to client
            $command = "openticket";
            $values["clientid"] = $Order['userid'];
            $values["deptid"] = "2";
            $values["subject"] = "Order Cancelled";
            $values["message"] = "Hello,

            Your order has been cancelled.  This ticket is being opened to further communicate this with you." ;
            $values["priority"] = "Medium";
            $values['serviceid'] = (isset($Order['lineitems']['lineitem'][0]['relid']) ? $Order['lineitems']['lineitem'][0]['relid'] : null); // assign ticket ot first service in order

            $results = localAPI($command,$values);

        }
    }
});

 

 

 

 

Link to comment
Share on other sites

35 minutes ago, steven99 said:

Could you go in to more detail? 

We don't accept orders automatically, We want to send email notification if Pending order marked as cancelled with Template. (We will explain possible reasons why the order wasnt activated 

Link to comment
Share on other sites

Then something is not right with your hook as I have just tested the following hook and it sends out the notice when i cancel an order. 

<?php
/** @author steven99 **/
add_hook('CancelOrder', 1, function($vars) {
    $TicketOrEmail = "email"; // set to "email" for just email notification
    $EmailTemplateName = "Order Cancelled"; // set to name of the email template to use

    if (isset($vars['orderid']) and is_numeric($vars['orderid'])) // should be there, but defensive coding mode active
    {
        $results = localAPI('GetOrders', array('id' => $vars['orderid']));

        if ($results and isset($results['result']) AND $results['result'] == 'success' and isset($results['orders']['order'][0]))
        {
            $Order = $results['orders']['order'][0]; // supplied one id, get oen order
            //got the cancelled order, now what?

            if ($TicketOrEmail == "ticket")
            {
                //open ticket to client
                $command = "openticket";
                $values["clientid"] = $Order['userid'];
                $values["deptid"] = "1";
                $values["subject"] = "Order Cancelled";
                $values["message"] = "Hello,

            Your order has been cancelled.  This ticket is being opened to further communicate this with you." ;
                $values["priority"] = "Medium";
                $values['serviceid'] = (isset($Order['lineitems']['lineitem'][0]['relid']) ? $Order['lineitems']['lineitem'][0]['relid'] : null); // assign ticket ot first service in order

                $results = localAPI($command,$values);
            }
            elseif ($TicketOrEmail == "email")
            {
                $command = 'SendEmail';
                $postData = array(
                    'messagename' => $EmailTemplateName,
                    'id' => (isset($Order['lineitems']['lineitem'][0]['relid']) ? $Order['lineitems']['lineitem'][0]['relid'] : null),
                     'customvars' => base64_encode(serialize(array("Order"=>$Order))),

                );
                $results = localAPI($command,$postData);
            }

            /*
                * Order array;
                * Array
(
   [id] => 131
   [ordernum] => 3244838540
   [userid] => 4
   [contactid] => 0
   [date] => 2019-03-04 23:17:33
   [nameservers] =>
   [transfersecret] =>
   [renewals] =>
   [promocode] =>
   [promotype] =>
   [promovalue] =>
   [orderdata] => a:0:{}
   [amount] => 5.11
   [paymentmethod] => yourpayment
   [invoiceid] => 122
   [status] => Pending
   [ipaddress] => d
   [fraudmodule] =>
   [fraudoutput] =>
   [notes] =>
   [paymentmethodname] => gatewayname
   [paymentstatus] => Unpaid
   [name] => test
   [currencyprefix] => $
   [currencysuffix] =>  USD
   [frauddata] =>
   [lineitems] => Array
       (
           [lineitem] => Array
           (
               [0] => Array
                   (
                       [type] => product
                       [relid] => 77
                       [producttype] => Hosting Account
                       [product] => test
                       [domain] =>
                       [billingcycle] => Monthly
                       [amount] => WHMCS\View\Formatter\Price Object
                           (
                               [price:protected] => 5.11
                               [currency:protected] => Array
                                   (
                                       [id] => 1
                                       [code] => USD
                                       [prefix] => $
                                       [suffix] =>  USD
                                       [format] => 1
                                       [rate] => 1.00000
                                   )

                               [defaultFormat:protected] => {PREFIX}{PRICE}{SUFFIX}
                               [defaultCurrencyDescriptor:protected] => Array
                                   (
                                       [format] => 1
                                       [prefix] =>
                                       [suffix] =>
                                   )

                           )

                       [status] => Pending
               )
       )
   )
)
                */
        }
    }
});

Though for some reason the sendemail api isn't accepting the custom vars and doing {debug} in the email template it doesn't even show there.  If I have more time I'll look further at 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