i developed a module for HikaShop which shows product , now i have problem with that.the module does not follow component configuration.i set in HikaShop configuration when users click on "add to cart" stay on same page and show a notice but this module go to checkout directly.
this is my default file :
defined('_JEXEC') or die('Restricted access');
if(!empty($products)) :
?>
<div class="hikashop_tglproducts_module<?php echo $moduleclass_sfx; ?>" id="hikashop_tglproducts_module">
<div class="row">
<?php foreach ($products as $product): ?>
<div class="product-item col bg-white mx-1 p-2" id="product-<?php echo $product['product_id']; ?>">
<div class="product-img">
<img class="img-fluid" src="<?php echo $product['image']; ?>" alt="<?php echo $product['product_name']; ?>" />
</div>
<form class="form pt-2">
<div class="form-group row mb-2">
<label for="" class="col-sm-6 col-form-label"><?php echo $product['product_name']; ?></label>
<div class="col-sm-6">
<select class="form-control custom-select product-charactgleristic">
<?php $first = true; foreach ($product['characteristic'] as $ccItem): ?>
<option value="<?php echo $ccItem['id']; ?>" data-pid="<?php echo $ccItem['pid']; ?>" data-amount="<?php echo $ccItem['amount']; ?>" <?php if($first) {$first = false; echo 'selected="selected"'; } ?>>
<?php echo $ccItem['alias']; ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="lead text-center pb-2 product-price">
<?php echo $currency->format((isset($product['characteristic'][0]['amount']) ? $product['characteristic'][0]['amount'] : 0), hikashop_getCurrency()); ?>
</div>
<div class="form-group row">
<div class="input-group col-sm-12">
<div class="input-group-append product-qinc">
<span class="input-group-text">+</span>
</div>
<input type="text" class="form-control text-center product-quantity" placeholder="<?php echo JText::_('MOD_HIKASHOP_TGLPRODUCTS_COUNT'); ?>" value="1">
<div class="input-group-prepend product-qdec">
<span class="input-group-text">-</span>
</div>
</div>
</div>
<a class="btn btn-block hikabtn hikacart tglproduct_addtocart" href="index.php/component/hikashop/product/updatecart/add-1/cid-<?php echo (isset($product['characteristic'][0]['pid']) ? $product['characteristic'][0]['pid'] : $product['product_id']); ?>/quantity-1"><?php echo JText::_('MOD_HIKASHOP_TGLPRODUCTS_ADD_TO_CART'); ?></a>
</form>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif;
and this is module php file:
<?php
defined('_JEXEC') or die('Restricted access');
if(!defined('DS'))
define('DS', DIRECTORY_SEPARATOR);
if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS) . DS . 'components' . DS . 'com_hikashop' . DS . 'helpers' . DS . 'helper.php'))
{
echo 'This module can not work without the Hikashop Component';
return;
}
$params->set('from_module', $module->id);
hikashop_initModule();
$config =& hikashop_config();
if(empty($module_options)){
$module_options = $config->get('default_params');
}
$hikashopOptions = $params->get('hikashopmodule', array());
// Get category id
$category = ctype_digit($hikashopOptions->selectparentlisting) ? $hikashopOptions->selectparentlisting : false;
// Include the module helper class only once
JLoader::register('ModHikashopTglProductsHelper', __DIR__ . '/helper.php');
$moduleclass_sfx = htmlspecialchars($params->get('moduleclass_sfx'), ENT_COMPAT, 'UTF-8');
$limit = ctype_digit($params->get('productlimit', false)) ? $params->get('productlimit') : false;
$items = ModHikashopTglProductsHelper::getProducts($category, $limit);
$products = array();
foreach ($items as $item)
{
if($item->product_parent_id == 0)
{
$products[$item->product_id]['product_id'] = $item->product_id;
$products[$item->product_id]['product_name'] = $item->product_name;
$products[$item->product_id]['product_description'] = $item->product_description;
$products[$item->product_id]['product_quantity'] = $item->product_quantity;
$products[$item->product_id]['product_code'] = $item->product_code;
if(!isset($products[$item->product_id]['characteristic']) ||
!is_array($products[$item->product_id]['characteristic']))
$products[$item->product_id]['characteristic'] = array();
$images = explode(',', $item->images);
if(isset($images[0]) && !empty($images[0]))
$products[$item->product_id]['image'] = 'images/com_hikashop/upload/' . $images[0];
else
$products[$item->product_id]['image'] = 'images/no-pic.jpg';
}
else
{
//characteristic
if(!isset($products[$item->product_parent_id]['characteristic']) ||
!is_array($products[$item->product_parent_id]['characteristic']))
$products[$item->product_parent_id]['characteristic'] = array();
array_push($products[$item->product_parent_id]['characteristic'], array(
'id'=> $item->characteristic,
'pid'=> $item->product_id,
'alias'=> $item->characteristicAlias,
'amount'=> $item->product_sort_price
));
}
}
$currency = hikashop_get('class.currency');
require JModuleHelper::getLayoutPath('mod_hikashop_tglproducts', $params->get('layout', 'default'));
The key here is the line:
<a class="btn btn-block hikabtn hikacart tglproduct_addtocart" href="index.php/component/hikashop/product/updatecart/add-1/cid-<?php echo (isset($product['characteristic'][0]['pid']) ? $product['characteristic'][0]['pid'] : $product['product_id']); ?>/quantity-1"><?php echo JText::_('MOD_HIKASHOP_TGLPRODUCTS_ADD_TO_CART'); ?></a>
You have only implemented the fallback mechanism of the HikaShop add to cart, which is the direct add to cart link. In that case, the javascript of the add to cart system of HikaShop is not used and thus there is no dynamic refresh on the current page.
If you want to use the javascript add to cart system to be able to add to the cart without changing page, you need to change your code to something like that :
<?php $product_id = (isset($product['characteristic'][0]['pid']) ? $product['characteristic'][0]['pid'] : $product['product_id']); ?>
<a class="btn btn-block hikabtn hikacart tglproduct_addtocart" href="index.php/component/hikashop/product/updatecart/add-1/cid-<?php echo $product_id; ?>/quantity-1" onclick="if(window.hikashop.addToCart) { return window.hikashop.addToCart(this); }" data-addToCart="<?php echo $product_id; ?>" data-addTo-div="MY_FORM_NAME" data-addTo-class="add_in_progress"><?php echo JText::_('MOD_HIKASHOP_TGLPRODUCTS_ADD_TO_CART'); ?></a>
You also want to add the name MY_FORM_NAME to your form tag (it needs to be dynamic of course). Since you already load the main helper file of HikaShop, you won't need to load the javascript/CSS of HikaShop.