Author Topic: Own invoice template (or override VM core class/function)  (Read 267 times)

caesarsk

  • Jr. Member
  • **
  • Posts: 50
Own invoice template (or override VM core class/function)
« 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
Code: [Select]
/components/com_virtuemart/controllers/invoice.php
is called function
Code: [Select]
return VmPdf::createVmPdf($view, $path, 'F', $metadata);
from file
Code: [Select]
/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
Code: [Select]
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
Code: [Select]
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
Code: [Select]
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

GJC Web Design

  • 3rd party VirtueMart Developer
  • Super Hero
  • *
  • Posts: 8433
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 2.6.22 & 3.2.14
Re: Own invoice template (or override VM core class/function)
« Reply #1 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
GJC Web Design
VirtueMart and Joomla Developers - php developers http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 3466
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3.0.x.y
Re: Own invoice template (or override VM core class/function)
« Reply #2 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

caesarsk

  • Jr. Member
  • **
  • Posts: 50
Re: Own invoice template (or override VM core class/function)
« Reply #3 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).

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 3466
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3.0.x.y
Re: Own invoice template (or override VM core class/function)
« Reply #4 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
Code: [Select]
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

caesarsk

  • Jr. Member
  • **
  • Posts: 50
Re: Own invoice template (or override VM core class/function)
« Reply #5 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.