使用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?

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.

Change the parameters int the function

function addItemsToCart(option_id, option_value)

In the data change

data: {product_id: 92, quantity: 1, option_id: option_id, option_value: option_value},

Change the buttons

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

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

In the post values will be like this:

$this->request->post = Array(
    ['product_id'] = 92,
    ['quantity'] = 1,
    ['option_id'] = 17,
    ['option_value'] = 83
)