[已解决]
在我的所有产品页面(不是类别页面)上 - 当您点击“添加”时
到购物车'它会添加 x2 的所有内容。
您将拥有两个用于控制添加到购物车按钮的功能。正确放置/执行代码会产生不同的结果。请参阅下面的代码 sn-ps。
第一个在 /catalog/view/javascript/common.js 中找到:
function addToCart(product_id) {
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: 'product_id=' + product_id,
dataType: 'json',
success: function(json) {
第二个通常位于 /catalog/view/theme/template/yourtheme/product/product.tpl:
$('#button-cart').bind('click', function() {
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
dataType: 'json',
success: function(json) {
如果您会注意到代码的 data: 部分。一种是检查选项,然后添加到购物车,另一种是添加到购物车。
这个sn-p的代码data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
正在检查选项,然后添加到购物车。这通常用于产品详细信息页面 (product.tpl)。
这个代码data: 'product_id=' + product_id, 的sn-p 直接将产品添加到购物车,通常用于网站的其他地方(产品详细信息页面除外)。用户只需将商品添加到购物车然后结帐即可。
您将 2x 项 添加到购物车的原因是因为您使用的是 sn-p 代码检查选项然后添加到购物车。要使任何添加到购物车按钮仅将一件商品放入购物车(不检查选项),您需要使用data: 'product_id=' + product_id,
看起来像这样
$('#button-cart').bind('click', function() {
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: 'product_id=' + product_id,
dataType: 'json',
success: function(json) {