简体   繁体   English

添加到具有产品ID的购物车功能

[英]Add to cart function with product ID's

Ive built a custom product builder for my site, and am stuck at the very last step - adding the products to the cart. Ive为我的网站构建了一个自定义产品构建器,并停留在最后一步-将产品添加到购物车。

so on my custom template file I have this code firing when the 'Add to Cart' button is clicked: 因此,在我的自定义模板文件上,单击“添加到购物车”按钮时会触发以下代码:

$('#button-cart').bind('click', function() {
    $.ajax({
    url: 'index.php?route=checkout/cart/add',
    type: 'post',
    data: $('I WANT TO PUT PRODUCT ID HERE, BUT DOESNT WORK'),
    dataType: 'json',
    success: function(json) {

    }
});

I have the product id saved as a javascript variable, I just need to actually get it to add this product to the cart. 我将产品ID保存为javascript变量,我只需要实际获取它即可将此产品添加到购物车中。 Any help would be great. 任何帮助都会很棒。 Thanks. 谢谢。

_____________ EDIT ______________ _____________编辑______________

This is my most recent code, the function is definitely being fired when I click the add to cart button, locketID is simply a product id. 这是我最近的代码,当我单击“添加到购物车”按钮时,肯定会触发该函数,locketID只是一个产品ID。 The success function is being called but neither of the if statements (error or success) and being executed, and the product isn't being added to to the cart. 成功函数将被调用,但不会执行任何if语句(错误或成功),并且不会将产品添加到购物车中。

Is it worth mentioning that this is happening on the category template file? 值得一提的是,这是在类别模板文件上发生的吗? Is there anything that needs included that might not be? 是否有需要的东西可能没有?

$('#button-cart').bind('click', function() {
    $.ajax({
        url: 'index.php?route=checkout/cart/add',
        type: 'post',
        data: locketID,
        dataType: 'json',
        success: function(json) {

            console.log('success function called')

            if (json['error']) {
                console.log('ERROR');
            } 

            if (json['success']) {
                console.log('SUCCESS');
            }


        }
    });
});

___________EDIT__________ ___________编辑__________

So, I've taken a slightly different approach this is what I have in the being called on the add to cart button click, but it redirects to the product page rather than add the product to the cart, any ideas? 因此,我采用了一种略有不同的方法,即单击“添加到购物车”按钮时所调用的方法,但是它重定向到产品页面,而不是将产品添加到购物车,有什么想法吗?

$('#button-cart-custom').bind('click', function() {

    var quantity = 1;
    var product_id = 12196;

    $.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'); 
            }   
        }
    });


});

It's very simple 很简单

var vars = new Array();

vars = {var1: var1_value,var2: var2_value}

now replace in your code 现在替换您的代码

data: $('I WANT TO PUT PRODUCT ID HERE, BUT DOESNT WORK'), with data: vars; data: $('I WANT TO PUT PRODUCT ID HERE, BUT DOESNT WORK'), data: vars;

replace data: locketID with 用以下内容替换data: locketID

data:{'id':locketID}  

and use 和使用

$_POST['id'] to get that data on server $_POST['id']在服务器上获取该数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM