我为HikaShop开发了一个显示产品的模块,现在我有问题。该模块没有按照组件配置。我在HikaShop配置中设置当用户点击“添加到购物车”时保持在同一页面并显示通知但是这个模块直接去结账。
这是我的默认文件:
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;
这是模块php文件:
<?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'));
答案 0 :(得分:1)
这里的关键是:
<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>
您只实现了HikaShop添加到购物车的后备机制,这是直接添加到购物车链接。在这种情况下,不使用HikaShop的添加到购物车系统的javascript,因此当前页面上没有动态刷新。
如果您想使用javascript add to cart系统无需更改页面即可添加到购物车,则需要将代码更改为:
<?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>
您还希望将名称MY_FORM_NAME添加到表单标记中(当然,它需要是动态的)。 由于您已经加载了HikaShop的主助手文件,因此您无需加载HikaShop的javascript / CSS。