Author Topic: Create functions accessible to my VM template files?  (Read 1903 times)

EvanGR

  • Jr. Member
  • **
  • Posts: 281
Create functions accessible to my VM template files?
« on: January 22, 2019, 12:45:49 pm »
Hello,

Probably a silly question but here goes...

I need to create a set of variables, and functions, that I can access from my VM template files.

Probably a class or a trait. Not sure how I can include it in my VM template files for products and categories? Anyone care to give a quick and simple example?

Thanks

GJC Web Design

  • 3rd party VirtueMart Developer
  • Super Hero
  • *
  • Posts: 9220
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 3.6.8
Re: Create functions accessible to my VM template files?
« Reply #1 on: January 22, 2019, 13:29:51 pm »
create your php class file and include it in your template files by a standard php incl. call
or create a system plugin and call functions from that in your template files
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

PRO

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10377
  • VirtueMart Version: 3+
Re: Create functions accessible to my VM template files?
« Reply #2 on: January 22, 2019, 15:36:57 pm »
create a library

I will give you examples of how I use mine.


my library name is "Kaizen"
I have many classes in there.
1 class called "Product"
in my libraries/Kaizen folder. I have a php file called. product.php

start the file like so LibrarynameClassname

<?php class KaizenProduct{
// add some functions
// gets a product param from single plugin
//$product , $param_name
// separated  , choose a delimeter if you want to separated the strings.
public static function getParam($product,$param,$separated=''){
 $return='';
   if   (!empty($product->customfieldsSorted['addtocart']))   {
     foreach ($product->customfieldsSorted['addtocart'] as $field) {
     if (!empty($field->$param)){
     $split='';
     if ($return!=''){$split=$separated;}
    $return.=$split.$field->$param;}

    }

    }
    return $return;
   // example KaizenProduct::getParam($product,'custom_drop',',');
}

 public static function get_product($id,$style,$header='',$thumbnail=150){
 if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if (!class_exists( 'VmModel' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'vmmodel.php');

$product_model = VmModel::getModel('product');
$product = $product_model->getProduct($id,TRUE,TRUE,TRUE,1);
$product_model->addImages($product);
$customfieldsModel = VmModel::getModel ('Customfields');
$product->customfields = $customfieldsModel->getCustomEmbeddedProductCustomFields ($product->allIds,0,-1, true);
$name=$product->product_name;
$link= JRoute::_('index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id='.$id.'&virtuemart_category_id='.$product->virtuemart_category_id.'');

$html='<div class="kaizen_product_display '.$style.'">';
if (!empty($header)){
$html.='<p class="featured_label">'.$header.'</p>';
}
$html.='<a title="'.$product->product_name.'" href="'.$link.'">';
$html.=$product->images[0]->displayMediaThumb('class="product-image"',false,'',true,false,false,$thumbnail,$thumbnail);
$html.='</a>';
$html.='<div class="wrap"></div>';
$html.='<a title="'.$product->product_name.'" href="'.$link.'">';
$html.= '<div class="name">'.$product->product_name.'</div>';
$html.='</a>';
$html.= '<div class="price redtext bold">'.KaizenProduct::priceCheck($product).'</div>';
$html.='<div class="wrap"></div></div>';


return $html;
 
 }

}
?>



THEN, YOU CAN USE THESE CODES ANYWHERE IN YOUR JOOMLA INSTALLATION BY DECLARING THE CLASS, AND USING THE FUNCTION.
You can also pre-register the class with a system plugin.

THE FILE YOU WANT TO USE IT IN.


You call the function by   LibrarynameClassname::functionname($parameters);
 if(!class_exists('KaizenProduct')){
              JLoader::discover('kaizen', JPATH_LIBRARIES . '/kaizen');
                }

   echo KaizenProduct::get_product($product_id,' center border',' Featured Product');

J3.9+ VM 3.4.2
Slowest Page Speed Score (88) (Category)
Fastest Page Speed Score (94-96) (productdetails)

EvanGR

  • Jr. Member
  • **
  • Posts: 281
Re: Create functions accessible to my VM template files?
« Reply #3 on: January 23, 2019, 08:26:15 am »
Awesome PRO, thank you very much!