VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: caesarsk on October 30, 2018, 09:39:24 AM

Title: Own invoice template (or override VM core class/function)
Post by: caesarsk on October 30, 2018, 09:39:24 AM
Hi all,

this post will be essentially the same as http://forum.virtuemart.net/index.php?topic=141282.0

Situation:
I want use my own invoice style.
In default, from file /components/com_virtuemart/controllers/invoice.php
is called function return VmPdf::createVmPdf($view, $path, 'F', $metadata);
from file /components/com_virtuemart/helpers/vmpdf.php

To use my own invoice template, I created a new class in this file that is called to the original one. The template that is generated by this class is in the attachment.

//modified VM code

class VmPdf {
static function createVmPdf($view, $path='', $dest='F', $meta=array()) {
if(!$view){
return;
}

if(!class_exists('VmVendorPDF')){
return 0;
}

$pdf = new VmVendorPDF();
//default
/*if (isset($meta['title'])) $pdf->SetTitle($meta['title']);
if (isset($meta['subject'])) $pdf->SetSubject($meta['subject']);
if (isset($meta['keywords'])) $pdf->SetKeywords($meta['keywords']);*/
// Make the formatter available, just in case some specialized view wants/needs it
$view->pdf_formatter = $pdf;
$view->isPdf = true;
$view->print = false;

ob_start();
$view->display();
$html = ob_get_contents();
ob_end_clean();

//added
$pohodaFa = new pohodaFa();
$getVendor = $pohodaFa->getVendorInfo();
$getDesc = $pohodaFa->getInvoiceDesc();
$getOrderInfo = $pohodaFa->getOrderInfo($view->invoiceNumber);
$getUser = $pohodaFa->getUserInfo($getOrderInfo['user_id']);
$getOrderItems = $pohodaFa->getOrderItems($getOrderInfo['id']);

// lot of my code

$pdf->Output($path, $dest);

return $path;
}
}


//my class
class pohodaFa {
        //this function is ignored
/*public function createInvoice($view, $path='', $dest='F', $meta=array()){
}*/
public function header($pdf,$getVendor,$getUser,$getDesc,$getOrderInfo,$order_date,$order_number){

return $header;
}
public function subHeader($pdf,$getDesc,$corY = 125,$border_height=156){

return $subheader;
}
public function bodyFooter($pdf,$getDesc,$getOrderInfo,$n=154){

return $bodyFooter;
}
public function footer($pdf,$getDesc,$getOrderInfo,$invoiceNumber=''){

return $footer;
}
public function getVendorInfo(){

return $vendor;
}
public function getUserInfo($user_id=false){

return $user;
}
public function getOrderInfo($invoiceNumber=false){

return $order;
}
public function getOrderItems($order_id=false){

}
public function getInvoiceDesc(){

return $desc;
}
}


Now questions:
1. If I try only rename default VM class/function eg. VmPdfblabla (I have modified it everywhere. In invoice.php and VmPdf.php) - VM generate error non-existent class. Why? If I try use this
return pohodaFa::createInvoice($view, $path, 'F', $metadata); VM return same error (function createInvoice is now ignored because I call other functions from default createVmPdf)
2. I will lose these modifications by updating VM (of course). It is possible override the VM core functions and use my own class/functions (so that the original ones are totally ignored)? For example, using the VMcustom plugin.

What I want to achieve:
- for example in override template folder (/templates/html/ or use VMcustom) save my php file with my own class for generate my invoice template, eg. /templates/html/com_virtuemart/vmpdf.php (with my class)
- this (my) class must replace the original class VmPdf (the original class must be ignored or deactivated)
- the modification must be without corehack (because updates, because corehack, because...)

Its possible?
If I did not add some facts, ask. I will add it.


thx
Title: Re: Own invoice template (or override VM core class/function)
Post by: GJC Web Design on October 30, 2018, 11:35:20 AM
plugins can only be used if u can find an appropriate trigger or use a system plugin with filtering to try and find that view

for over rides not in core files see:  http://forum.virtuemart.net/index.php?topic=141282.msg497442#msg497442
Title: Re: Own invoice template (or override VM core class/function)
Post by: Studio 42 on October 30, 2018, 20:11:09 PM
you can add a Joomla system plugin and load (using require or require_once) your own class having the same name as the Virtuemart class.
The VM class is then ignored(because you already declared the class before)
Add all your class overrides in your plugin folder and do a plugin package and all should work.
See https://docs.joomla.org/Plugin/Events/System for the right trigger and a sample plugin
Title: Re: Own invoice template (or override VM core class/function)
Post by: caesarsk on October 30, 2018, 20:39:55 PM
ou, thx. This is exactly what I wanted to hear. So I understand correctly. With one plugin, I can solve multiple VM modifications like invoice template, product.php corehack etc. yes?

If interested, I can provide the code for this invoice template (use at your own risk).
Title: Re: Own invoice template (or override VM core class/function)
Post by: Studio 42 on October 30, 2018, 23:40:52 PM
You can do it for any Vm classes, but you have to load the vm config and add some other codes
class plgSystemExample extends JPlugin
{

/**
* Listener for the `onAfterRoute` event
*
* @return  void
*
* @since   1.0
*/
public function onAfterRoute()
{
//load Vm framework so helper and models can be load correctly
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
if (!class_exists( 'VmConfig' )) require(JPATH_ROOT .'/administrator/components/com_virtuemart/helpers/config.php');
VmConfig::loadConfig();
//load class
require JPATH_ROOT.'/plugins/system/example/overrides/file.php';
//additional code here if needed
}
}

Note that you should verify if admin or front, not load classe when it's not needed ..
The code is the minimum
Title: Re: Own invoice template (or override VM core class/function)
Post by: caesarsk on October 31, 2018, 02:06:11 AM
yep, I'm already studying and overwriting it. If I'm done, plugin will be published here for others and its further use.
thank you very much for navigating me in the right direction.