Page 1 of 1

Problem with calling ajax function in custom page

Posted: Wed Feb 13, 2013 12:01 am
by imsir
I have opencart. function addToCart() that adds products to cart works normal on normal pages.

I made custom page where I have just button that triggers function addtoCart(). Just normal button..

On normal page(where it works) I get normal response.

Post and response of addToCart() that works on normal page(category, product):

Post for custompage is the same as normal page. But this is response of addToCart() function on custom page(information/custompage):

Therefore item is not added to cart.
Do you have an idea why is this happening?

I use function addToCart() and use hard variable FOR TEST in that custom page, which means variables are always there to pass. I use hard variables so I don't have to explain how do I pass variables back in code(it works the same, it passes everything in debug). Problem is I get that "empty" response back only on custom made page. Response: [] ...

Just if I use for example(this is example site, but works same on my site): ... quantity=1

Try this for example. It returns [].. Why? What is the point? Do I have to register permisson for usings addToCart() on different pages?

Code: Select all

function addToCart() {

    var product_id = 79;
    var quantity = 1;

        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>');



                $('html, body').animate({
                    scrollTop: 0
                }, 'slow');

Re: Problem with calling ajax function in custom page

Posted: Wed Feb 13, 2013 2:06 am
by imsir
Ok found a solution after 1 day struggling.

So I use custom page, to add custom products to database and than imediatelly to cart.

When I called addProduct to shop before I triggered addToCart() function for that product I tried to minimize attributes needed for adding product.

Therefore I commented store_id. But you need to have your custom product in some store. If product doesn't have store_id, you can't add him into cart.

If product doesn't have store_id it can't be found under product/product&product_id=ID, therefore you can't add it in cart.

gg no re, volevu cya thanks