Shoora
11/6/2015 - 5:06 AM

opencart external

opencart external


How do I add a product to OpenCart from an external site?
up vote
0
down vote
favorite
	

I need to add a product to the shopping cart from a website to ocart. I was trying the following code. All I get is "shopping cart empty" after the user clicks the submit button.

I made sure that the product id = 40 does exist. thansk for any help

<form action="http://***.com/purchase/index.php?route=checkout/cart" id="personalVirtualPrivateServerForm" method="post">
<input type="hidden" name="product_id" value="40">
<input type="hidden" name="quantity" value="2">
<input type="submit" alt="Order Now" title=" value="Order Now">
</form>



The form action should be

http://***.com/purchase/index.php?route=checkout/cart/add
           mind this particular action being called -^^^^

However I think this won't work as the method called ControllerCheckoutCart::add() is expected to work with AJAX request returning the JSON response. So if You submit a form to this URL instead of shopping cart being displayed it will display only the JSON response.

Instead of direct submitting the form You should make sure it is submitted by jQuery AJAX after the submit button was clicked. Then You can redirect the user to the shopping cart on success. Here is possible solution, make sure to fill in the real domain. It is not tested. Place this script on the page where the form is present (supposes jQuery is linked to the site):

$(document).ready(function() {
    $('form#personalVirtualPrivateServerForm input[type="submit"]').click(function(e) {
        e.preventDefault(); // prevent the form from submitting

        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: 'http://.../index.php?route=checkout/cart/add'
            data: 'product_id=' + $('form#personalVirtualPrivateServerForm input[name="product_id"]').val() + '&quantity=' + $('form#personalVirtualPrivateServerForm input[name="quantity"]').val(),
            success: function(json) {
                window.location = 'http://.../index.php?route=checkout/cart';
            }
        });
    });
});