Jump to content

override for HistoryOrder.php


ppokrovsky

Recommended Posts

Hi.
 
I'm trying to add some new variables to email templates sent when order status has changed. I've overridden addWithemail method in HistoryOrder class by creating /override/classes/order/OrderHistory.php with following contents

<?php

class OrderHistory extends OrderHistoryCore
{

    public function addWithemail($autodate = true, $template_vars = false, Context $context = null)
    {
        if (!$context)
            $context = Context::getContext();
        $order = new Order($this->id_order);

        if (!$this->add($autodate))
            return false;

        $result = Db::getInstance()->getRow('
			SELECT osl.`template`, c.`lastname`, c.`firstname`, osl.`name` AS osname, c.`email`, os.`module_name`, os.`id_order_state`
			FROM `'._DB_PREFIX_.'order_history` oh
				LEFT JOIN `'._DB_PREFIX_.'orders` o ON oh.`id_order` = o.`id_order`
				LEFT JOIN `'._DB_PREFIX_.'customer` c ON o.`id_customer` = c.`id_customer`
				LEFT JOIN `'._DB_PREFIX_.'order_state` os ON oh.`id_order_state` = os.`id_order_state`
				LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = o.`id_lang`)
			WHERE oh.`id_order_history` = '.(int)$this->id.' AND os.`send_email` = 1');
        if (isset($result['template']) && Validate::isEmail($result['email']))
        {
            ShopUrl::cacheMainDomainForShop($order->id_shop);

            $topic = $result['osname'];
            $data = array(
                '{lastname}' => $result['lastname'],
                '{firstname}' => $result['firstname'],
                '{id_order}' => (int)$this->id_order,
                '{order_name}' => $order->getUniqReference()
            );
            if ($template_vars)
                $data = array_merge($data, $template_vars);

            if ($result['module_name'])
            {
                $module = Module::getInstanceByName($result['module_name']);
                if (Validate::isLoadedObject($module) && isset($module->extra_mail_vars) && is_array($module->extra_mail_vars))
                    $data = array_merge($data, $module->extra_mail_vars);
            }

            $data['{payment}'] = Tools::substr($order->payment, 0, 32);
            $data['{total_paid}'] = Tools::displayPrice((float)$order->total_paid, new Currency((int)$order->id_currency), false);
            $data['{order_name}'] = $order->getUniqReference();

            if (Validate::isLoadedObject($order))
            {
                // Join PDF invoice if order status is "payment accepted"
                if ((int)$result['id_order_state'] === 2 && (int)Configuration::get('PS_INVOICE') && $order->invoice_number)
                {
                    $context = Context::getContext();
                    $pdf = new PDF($order->getInvoicesCollection(), PDF::TEMPLATE_INVOICE, $context->smarty);
                    $file_attachement['content'] = $pdf->render(false);
                    $file_attachement['name'] = Configuration::get('PS_INVOICE_PREFIX', (int)$order->id_lang, null, $order->id_shop).sprintf('%06d', $order->invoice_number).'.pdf';
                    $file_attachement['mime'] = 'application/pdf';
                }
                else
                    $file_attachement = null;

                Mail::Send((int)$order->id_lang, $result['template'], $topic, $data, $result['email'], $result['firstname'].' '.$result['lastname'],
                    null, null, $file_attachement, null, _PS_MAIL_DIR_, false, (int)$order->id_shop);
            }

            ShopUrl::resetMainDomainCache();
        }

        return true;
    }

}

The only difference here with HistoryOrderCore class is that I added

$data['{payment}'] = Tools::substr($order->payment, 0, 32);

which is meant to get resolved in payment way.

 

The override approach seemed pretty straightforward

 

When i do similar changes to OrderHistoryCore class everything seem to work, but for some reason changes are not reflected when they are made in override as if Presta doesn't recognize this override.

 

What am I missing here?

 

PS 1.6.0.8

 

Thanks!

Edited by ppokrovsky (see edit history)
Link to comment
Share on other sites

Okay, another glorious victory. It turns out the issue was that override was not in cache/class_index.php file

 

w/o override it looked like

$ cat cache/class_index.php  | grep History
  'OrderHistory' => 
  'OrderHistoryCore' => 
    'path' => 'classes/order/OrderHistory.php'

I deleted class_index.php, and after rebuild it looks like

$ cat cache/class_index.php  | grep History
  'OrderHistory' => 
    'path' => 'override/classes/order/OrderHistory.php',
  'OrderHistoryCore' => 
    'path' => 'classes/order/OrderHistory.php'

and all the changes from override were applied correctly.

 

I'm not quite sure what causes such behavior, maybe class_index.php is meant to get rebuild automatically?

Link to comment
Share on other sites

thats correct, the class_index is supposed to be updated at a certain interval when PS detects changes.  I'm not exactly sure what the interval is, but what you have done is one of the most asked questions about overrides that most people do not realize.

 

Now what I would suggest doing is the following instead.  This way you do not need to redo your override when you upgrade PS and the OrderHistory class changes.

<?php

class OrderHistory extends OrderHistoryCore
{

    public function addWithemail($autodate = true, $template_vars = false, Context $context = null)
    {
        if (!$context)
            $context = Context::getContext();

        $order = new Order($this->id_order);
        $data = array(
            '{payment}' => Tools::substr($order->payment, 0, 32),
        );

        if ($template_vars)
            $data = array_merge($data, $template_vars);

        return parent::addWithemail($autodate, $data, $context);

    }
}
Link to comment
Share on other sites

Actually, documentation is quite clear on class_index.php regeneration

 

 

Modules and themes may add an override to a default behavior, and PrestaShop takes care of reseting the /cache/class_index.php file.

But sometimes you need to add that overriding code yourself, manually uploading the file to your server. In that case, you need to trigger the regeneration of the /cache/class_index.php file yourself. This is done simply by deleting the file: if PrestaShop cannot find the file, it will regenerate it, taking all the overrides into account.

It is the same when manually removing an override: in order to reinstate the default behavior, you must delete the /cache/class_index.php file.

 

Since override is done manually in this case, /cache/class_index.php must be deleted manually to regenerate.

Link to comment
Share on other sites

  • 3 years later...

very instructive post.

 

in case i want to override the sendEmail function in PS 1.6.1.14 this is correct?

 

<?php


class OrderHistory extends OrderHistoryCore
{


    public function sendEmail($order, $template_vars = false)
    {
        $result = Db::getInstance()->getRow('
            SELECT osl.`template`, c.`lastname`, c.`firstname`, osl.`name` AS osname, c.`email`, os.`module_name`, os.`id_order_state`, os.`pdf_invoice`, os.`pdf_delivery`
            FROM `'._DB_PREFIX_.'order_history` oh
                LEFT JOIN `'._DB_PREFIX_.'orders` o ON oh.`id_order` = o.`id_order`
                LEFT JOIN `'._DB_PREFIX_.'customer` c ON o.`id_customer` = c.`id_customer`
                LEFT JOIN `'._DB_PREFIX_.'order_state` os ON oh.`id_order_state` = os.`id_order_state`
                LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = o.`id_lang`)
            WHERE oh.`id_order_history` = '.(int)$this->id.' AND os.`send_email` = 1');    
            
        if (isset($result['template']) && Validate::isEmail($result['email'])) {
            
            $data = array(
                '{mail}' => $result['mail'],
            );


            if ($template_vars) {
                $data = array_merge($data, $template_vars);
            }
        }
     return parent::sendEmail($order, $data);


    }
}

i just added {mail} as a new placeholder variable.

 

ty for any help

Edited by ANGELO Vintage (see edit history)
  • Like 1
Link to comment
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
×
×
  • Create New...