News:

Looking for documentation? Take a look on our wiki

Main Menu

Create functions accessible to my VM template files?

Started by EvanGR, January 22, 2019, 12:45:49 PM

Previous topic - Next topic

EvanGR

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

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 https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 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
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

PRO

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');


EvanGR