VirtueMart Forum

VirtueMart 2 + 3 => Templating & Layouts => Topic started by: EvanGR on January 22, 2019, 12:45:49 pm

Title: Create functions accessible to my VM template files?
Post by: EvanGR 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
Title: Re: Create functions accessible to my VM template files?
Post by: GJC Web Design 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
Title: Re: Create functions accessible to my VM template files?
Post by: PRO 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');

Title: Re: Create functions accessible to my VM template files?
Post by: EvanGR on January 23, 2019, 08:26:15 am
Awesome PRO, thank you very much!