Joomla Virtuemart custom plug-in to make multi-variant product

Joomla Virtuemart custom plug-in to make multi-variant product

A Multi-variant product is a product that is available in multiple variants(like – color, size, etc.), and price of the product varies, based on particular variant selection by any buyer, though the actual product id is same for all. Joomla VirtueMart apart from other custom fields allows application of custom field plug-ins onto its products. These additional custom fields plug-ins can be used to provide additional properties to a product, like – dynamic product range selection based on a variant selection.
Custom plugin in Joomla VirtueMart falls under the vmcustom group of plugins, so the base class for it will be named as plgVmCustomYourPluginName that extends class vmCustomPlugin. The structure for constructor would come as –

/**
 * Webkul Software.
 *
 * @category  Webkul
 * @author    Webkul
 * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

class plgVmCustomYourPluginName extends vmCustomPlugin {
	function __construct(& $subject, $config) {
		parent::__construct($subject, $config);
		$varsToPush = array(
			'var_1'=>array(0,'bool'),
			'var_2'=>array('','string'),
			'var_3'=>array(array(),'array'),
			'var_4'=>array(0,'int')
		);
		$this->setConfigParameterable('customfield_params', $varsToPush);
	}

We will use var_1 as a check input to change product price, and var_2 as the price to be added dynamically.
The vars used that are set in config parameters are then can be accessed by object ‘$field’ and can be edited by any input field with name=”customfield_params[<?php echo $row ?>][var_2]” on product edit page in backend as-

/**
 * Webkul Software.
 *
 * @category  Webkul
 * @author    Webkul
 * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

/**
 * plgVmOnProductEdit get product param for this plugin on edit.
 * Displays the custom field in the product view of the backend
 * The custom field should not be displayed before the product being saved. 
 * Also should not loaded in the child products
 * 
 * @param  object $field - The custom field     
 * @param  int $product_id virtuemart product id
 * @param  int $row - The a/a of that field within the product      
 * @param  string $retValue  - The html that regards the custom fields of that product
 * 
 * @return boolean true
 * @since	1.0
 */
function plgVmOnProductEdit($field, $product_id, &$row, &$retValue) {
	if ($field->custom_element != $this->_name) return '';
	ob_start(); ?>
	<table class="adminlist">
		<tbody>
			<!-- making any field in product edit -->
			<tr>
				<td>
					<input type="checkbox" value="1"<?php if (!empty($field->var_1)) { ?> checked="checked"<?php } ?> name="customfield_params[<?php echo $row; ?>][var_1]" />
				</td>
				<td>
					<?php 
					$var_2 = empty($field->var_1) ? '' : $field->var_2; ?>
					<input type="text" name="customfield_params[<?php echo $row ?>][var_2]" value="<?php echo $var_1 ?>" />
				</td>
			</tr>
		</tbody>
	</table>
	<?php
	$retValue .= ob_get_clean();
	$row++;
	return true;
}

Similarly, these filled vars are then can be accessed by object ‘$group’ and can be edited by any input field with name=”customProductData[<?php echo $row ?>][AnyUniqueName]” on product edit page in backend. Here we are creating a check box, on the selection of which we will change product price.

/**
 * Webkul Software.
 *
 * @category  Webkul
 * @author    Webkul
 * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

/**
 * plgVmOnDisplayProductFEVM3 method to generate output on the product page in FE. 
 * Display of the Cart Variant/Non cart variants Custom fields - VM3
 * 
 * @param  object $product Virtuemart Product
 * @param  object $group   Virtuemart custom field
 */
function plgVmOnDisplayProductFEVM3(&$product,&$group) {
	if ($group->custom_element != $this->_name) 
		return '';
	$anyUniqueName = '';
	ob_start(); ?>
	<div class="row">
		<div class="wk-rent-sel-des">
			<p><?php echo $group->additional_charge_title==''?JText::_('PLG_JVBRES_MOSD_ADDITIONAL_CHARGE'):$group->additional_charge_title ?></p>
		</div>
		<div class="wk-rent-sel">
			<input type="checkbox" name="customProductData[<?php echo $group->virtuemart_product_id; ?>][<?php echo $group->virtuemart_custom_id; ?>][<?php echo $group->virtuemart_customfield_id; ?>][anyUniqueName]" value="<?php echo $group->var_1; ?>" />
		</div>
	</div>
	<?php
	$group->display .= ob_get_clean();
}

As you can see, we initialized a variable $anyUniqueName and then used it as an index to the input field. This is done to get the value of it at runtime, so that product price can be changed accordingly. We can do this with the help of modificatorSum in the listed function –

/**
 * Webkul Software.
 *
 * @category  Webkul
 * @author    Webkul
 * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

/**
 * plgVmPrepareCartProduct previously plgVmCalculateCustomVariant() 
 * use it to modfiy the product in your cart, price, weight, dimension, text, name, whatever. 
 * To modify the price of a product with a custom field. 
 * Call from ajax, when custom field value change while selecting product for cart.
 * 
 * @param  object $product        TableProducts
 * @param  object $customfield    virtuemart custom field
 * @param  array $selected        keeps the data of the right method/plugin coming from the form
 * @param  float $modificatorSum  variable to be modified
 * 
 * @return boolean                true
 */
public function plgVmPrepareCartProduct(&$product, &$customfield, $selected, &$modificatorSum) {
	if ($customfield->custom_element !== $this->_name) 
		return ;
	if ($product->allPrices) 
		$modificatorSum -= $product->allPrices[0]['product_price'];
	$anyUniqueName = $this->dateInterpret(JArrayHelper::getValue($selected, 'anyUniqueName'));

	if ($anyUniqueName) {
		if ($customfield->var_2 > 0) {
			$modificatorSum += $customfield->var_2;
		}
	}
	return true;
}

Usage

A Multi-variant product is useful for many types of the stores that provide a range of products, as well as stores running booking and reservation concept, where a single product is managed based on different date/time selections by any buyer. One of this kind of extension is avaialble on Webkul store here:

Joomla Virtuemart Booking and Reservation System

For any query regarding Joomla VirtueMart plug-ins and add-ons, you can communicate with us at:
support@webkul.com