确定。我已经改变了smth,现在它几乎可以正常工作。只留下一个问题。 标题中的购物车链接(ID为 cart-total 的div)不再是ID 购物车的div。因此,当我删除购物车模式中的产品时,标题中的链接不会更新,就像它仍然有产品一样。
以下是新代码module/cart.tpl
<div class="heading">
<?php echo $heading_title; ?> <a data-toggle="modal" data-target="#cart"><span id="cart-total"> <?php echo $text_items; ?></span></a>
</div>
<div class="modal fade" id="cart" tabindex="-1" role="dialog" aria-labelledby="cartLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="content modal-content text-left">
<?php if ($products || $vouchers) { ?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="cartLabel"><?php echo $heading_title; ?></h4>
</div>
<div class="modal-body">
<table class="table table-hover mini-cart-info">
<?php foreach ($products as $product) { ?>
<tr>
<td class="image">
<?php if ($product['thumb']) { ?>
<a href="<?php echo $product['href']; ?>">
<img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" title="<?php echo $product['name']; ?>" />
</a>
<?php } ?>
</td>
<td class="name">
<a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a>
<?php foreach ($product['option'] as $option) { ?>
- <small><?php echo $option['name']; ?> <?php echo $option['value']; ?></small><br />
<?php } ?>
<?php if ($product['recurring']): ?>
- <small><?php echo $text_payment_profile ?> <?php echo $product['profile']; ?></small><br />
<?php endif; ?>
</td>
<td class="quantity">x <?php echo $product['quantity']; ?></td>
<td class="total"><?php echo $product['total']; ?></td>
<td class="remove"><a alt="<?php echo $button_remove; ?>" title="<?php echo $button_remove; ?>" onclick="(getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') ? location = 'index.php?route=checkout/cart&remove=<?php echo $product['key']; ?>' : $('#cart').load('index.php?route=module/cart&remove=<?php echo $product['key']; ?>' + ' #cart > *');" /><?php echo $button_remove; ?></a></td>
</tr>
<?php } ?>
<?php foreach ($vouchers as $voucher) { ?>
<tr>
<td class="image"></td>
<td class="name"><?php echo $voucher['description']; ?></td>
<td class="quantity">x 1</td>
<td class="total"><?php echo $voucher['amount']; ?></td>
<td class="remove"><img src="catalog/view/theme/default/image/remove-small.png" alt="<?php echo $button_remove; ?>" title="<?php echo $button_remove; ?>" onclick="(getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') ? location = 'index.php?route=checkout/cart&remove=<?php echo $voucher['key']; ?>' : $('#cart').load('index.php?route=module/cart&remove=<?php echo $voucher['key']; ?>' + ' #cart > *');" /></td>
</tr>
<?php } ?>
</table>
<div class="mini-cart-total">
<?php $i = count($totals); $i-- ; ?>
<?php echo $totals[$i]['title']; ?>: <?php echo $totals[$i]['text']; ?>
</div>
</div>
<div class="modal-footer checkout">
<a href="<?php echo $cart; ?>"><?php echo $text_cart; ?></a> | <a href="<?php echo $checkout; ?>"><?php echo $text_checkout; ?></a>
</div>
<?php } else { ?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="cartLabel"><?php echo $heading_title; ?></h4>
</div>
<div class="modal-body empty">
<?php echo $text_empty; ?>
</div>
<div class="modal-footer">
<a href="<?php echo $cart; ?>"><?php echo $text_cart; ?></a>
</div>
<?php } ?>
</div>
</div>
</div>
这是来自common.js
/* Ajax Cart */
$('.heading a').live('click', function() {
$('#cart').load('index.php?route=module/cart #cart > *');
});
function addToCart(product_id, quantity) {
quantity = typeof(quantity) != 'undefined' ? quantity : 1;
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: 'product_id=' + product_id + '&quantity=' + quantity,
dataType: 'json',
success: function(json) {
$('.success, .warning, .attention, .information, .error').remove();
if (json['redirect']) {
location = json['redirect'];
}
if (json['success']) {
$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
$('.success').fadeIn('slow');
$('#cart-total').html(json['total']);
$('html, body').animate({ scrollTop: 0 }, 'slow');
}
}
});
}
我将cart.tpl
中的所有代码与<div id="cart-container">
包装在一起
1.我已经改变了onClick
:
<a alt="<?php echo $button_remove; ?>" title="<?php echo $button_remove; ?>" onclick="(getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') ? location = 'index.php?route=checkout/cart&remove=<?php echo $product['key']; ?>' : $('#cart').load('index.php?route=module/cart&remove=<?php echo $product['key']; ?>' + ' #cart > *');" />
<?php echo $button_remove; ?>
</a>
为:
<a alt="<?php echo $button_remove; ?>" title="<?php echo $button_remove; ?>" onclick="(getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') ? location = 'index.php?route=checkout/cart&remove=<?php echo $product['key']; ?>' : $('#cart-container').load('index.php?route=module/cart&remove=<?php echo $product['key']; ?>' + ' #cart > *');" />
<?php echo $button_remove; ?>
</a>
它的工作方式如下:https://www.dropbox.com/s/q5o0x44z8mqytw4/1st.png 2.我再次更改了修改后的onClick
:
<a alt="<?php echo $button_remove; ?>" title="<?php echo $button_remove; ?>" onclick="(getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') ? location = 'index.php?route=checkout/cart&remove=<?php echo $product['key']; ?>' : $('#cart-container').load('index.php?route=module/cart&remove=<?php echo $product['key']; ?>' + ' #cart > *');" />
<?php echo $button_remove; ?>
</a>
为:
<a alt="<?php echo $button_remove; ?>" title="<?php echo $button_remove; ?>" onclick="(getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') ? location = 'index.php?route=checkout/cart&remove=<?php echo $product['key']; ?>' : $('#cart-container').load('index.php?route=module/cart&remove=<?php echo $product['key']; ?>' + ' #cart-container > *');" />
<?php echo $button_remove; ?>
</a>
答案 0 :(得分:0)
在common.js
文件中替换此js,然后进行检查。
function addToCart(product_id, quantity) {
quantity = typeof(quantity) != 'undefined' ? quantity : 1;
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: 'product_id=' + product_id + '&quantity=' + quantity,
dataType: 'json',
success: function(json) {
$('.alert-success, .alert-warning, .alert-attention, .alert-info, .alert-danger').remove();
if (json['redirect']) {
location = json['redirect'];
}
if (json['success']) {
$('#notification').html('<div class="alert alert-success alert-dismissible" role="alert">'
+ json['success'] + '<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button></div>');
$('.alert-success').fadeIn('slow');
$('#cart-total').html(json['total']);
$('#cart').load('index.php?route=module/cart #cart > *');
$('html, body').animate({ scrollTop: 0 }, 'slow');
}
}
});
}
答案 1 :(得分:0)
问题不在于JS,而在将产品添加到购物车(你从common.js
粘贴的JS代码)完全没有 - 你自己描述的问题是< em>当我删除产品时。
因此,你必须看一个完全不同的地方,那就是(为了更好的可读性我格式化了一点,你应该在你的模板中做同样的事情):
<td class="remove">
<a alt="<?php echo $button_remove; ?>" title="<?php echo $button_remove; ?>" onclick="
(getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout')
? location = 'index.php?route=checkout/cart&remove=<?php echo $product['key']; ?>'
: $('#cart').load('index.php?route=module/cart&remove=<?php echo $product['key']; ?>' + ' #cart > *');" /><?php echo $button_remove; ?></a>
</td>
现在问题本身就在于onclick
回调中的这段JS代码:
$('#cart').load('index.php?route=module/cart&remove=<?php echo $product['key']; ?>' + ' #cart > *');
此代码使用从请求的网址$('#cart')
返回的内容重新 index.php?route=module/cart&remove=<?php echo $product['key']; ?>
的内容。
这意味着,在您的HTML标记中,<div id="cart">
会被替换,而上面的div.heading
根本不会被触及{<1}}。
修复可能就是这个:
span#cart-total
中的整个代码包装在另一个带有id的div中,例如module/cart.tpl
cart-container
中的选择器替换为$('#cart').load(...)
。