dqpdb82600 2014-08-23 15:15
浏览 51

使用opencart中的ajax将产品手动添加到购物车

I need to add a product to the cart once the user click a button by using my own function.

The button code:

 <input type="button" value="Add to cart" onclick="addItemsToCart(83); " class="button btn-success"  id="addToCartButton1"/>

 <input type="button" value="Add to cart" onclick="addItemsToCart(84); " class="button btn-success"  id="addToCartButton2"/>

addItemsToCartFunction:

function addItemsToCart(option_value_id){

        $.ajax({
        url: 'index.php?route=checkout/cart/add',
        type: 'post',
        data: 'product_id=' + 92+ '&quantity=' + 1, //need to change this
        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'); 
            }   
        }
    });

    }

This is working properly.

But This product has a option called 'custom'.

option details according to the database:

option id: 17
option values ids 83 and 84.

I have passed the option value as a parameter to the function addItemsToCart.

Now I need to pass that option and value to the data section. That means I need to change this line.

data: 'product_id=' + 92+ '&quantity=' + 1,

How to do this?

  • 写回答

2条回答 默认 最新

  • dse55384 2014-08-23 15:21
    关注

    Since you take the parameter option_value_id, I think you want this:

    data: {product_id: option_value_id, quantity: 1},
    

    Check out jQuery Docs to learn more.

    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗