mass actionThe mass action system included in Hikashop allows developers to write their own plugin very easily in order to process data the way they want. This documentation will give you the basis to create a mass action plugin in order to exploit the capacities of the mass action system.

Two options are offered in this tutorial, you can either create an Hikashop plugin as explained in this tutorial (and then, in this plugin, add functions as explained in the following example=) or you can download our example plugin here, including the needed element to make a usable mass action plugin. We also highly encourage you to look at the existing mass action plugins that are already by default in HikaShop and which handle all the mass actions triggers, filters and actions for products, categories, orders, users and addresses that are available when you install HikaShop.

Template

Here is a  template example, a template allow you to display a new option in the triggers, filters or actions.

In this example we used the function "onMassactionTableFiltersLoad" so it will add an option in the filters.

To add an option in the triggers you have to use "onMassactionTableTriggersLoad", and "onMassactionTableActionsLoad" to add an action.

 

// Add a dropdown filter for the product type
function onMassactionTableFiltersLoad(&$table,&$filters,&$filters_html,&$loadedData){
	$type = 'filter';
	$massactionClass = hikashop_get('class.massaction');
	if(empty($loadedData->massaction_filters)){
		$loadedData->massaction_filters = array();
	}
	// Add the entry in the filters listing
	$filters['productType']=JText::_('PRODUCT_TYPE');
// Set the default values here
$loadedData->massaction_filters['__num__'] = new stdClass(); $loadedData->massaction_filters['__num__']->type = 'product'; $loadedData->massaction_filters['__num__']->data = array(); $loadedData->massaction_filters['__num__']->name = 'productType'; $loadedData->massaction_filters['__num__']->data['type'] = 'all'; $loadedData->massaction_filters['__num__']->html = ''; foreach($loadedData->massaction_filters as $key => &$value) { if($value->name != 'productType' || ($table->table != $loadedData->massaction_table && is_int($key))) continue; $value->type = 'product'; $product = hikashop_get('type.product'); $product->onchange='countresults(\''.$table->table.'\','.$key.');'; // add here the output, what will be displayed after selecting the filter
$output = $product->display('filter['.$table->table.']['.$key.'][productType][type]',$value->data['type'], 'chzn-done not-processed');
// call the "initDefaultDiv" function to format your content
$filters_html[$value->name] = $massactionClass->initDefaultDiv($value, $key, $type, $table->table, $loadedData, $output); } }


Once you have your template, you have to create the filter, you have to create the function "onProcessProductMassFilterproductType", where "Product" is corresponding to the data, "Filter" is the type of the added functionality (it could be "trigger" or "action"), and "productType" is the name of your filter.

// Create the function	
function onProcessProductMassFilterproductType(&$elements,&$query,$filter,$num){
	if(empty($filter['type']) || $filter['type']=='all') return;
	// If there is an element given, generally used when coming from a trigger
	if(count($elements)){
		foreach($elements as $k => $element){
			if($element->product_type!=$filter['type']) unset($elements[$k]);
		}
	}
	// Else there is no elements, so we get them from a request
	else{
		$db = JFactory::getDBO();
		$query->where[] = 'hk_product.product_type='.$db->Quote($filter['type']);
	 }
}

The following function count the number of results, it's displayed next to the selected filter. You have to name it "onCountProductMassFilterproductType", it's the same thing as before except that it's an onCount and not onProcess

function onCountProductMassFilterproductType(&$query,$filter,$num){
	$elements = array();
	// Call the main function to get the elements
	$this->onProcessProductMassFilterproductType($elements,$query,$filter,$num);
	return JText::sprintf('SELECTED_PRODUCTS',$query->count('hk_product.product_id'));
}

 

If you need to add an action or a trigger, it's working the same way.

You will just have to use the Trigger or Action functions instead of the Filter one.

 

To add a new data type, if you want to do mass actions on, for example, the discounts you can use the following code in your plugin:

	function onMassactionTableLoad(&$externalValues){
		$obj = new stdClass();
		$obj->table ='discount';
		$obj->value ='discount';
		$obj->text =JText::_('DISCOUNT');
		$externalValues[] = $obj;
	}