user2588945
user2588945

Reputation: 1681

How do I add a product to OpenCart from an external site?

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>

Upvotes: 0

Views: 3217

Answers (2)

dev7
dev7

Reputation: 6369

First off, you are using index.php?route=checkout/cart which is the controller to show the cart page. This does not actually add any product to the cart.

I'd approach your problem by either:

  1. shadyyx solution seems good. You may run into cross-domain policy issues though. Also you will always have to make sure you are using same protocol (https/http) otherwise browsers will block you. This can be frustrating.
  2. My prefered solution (and much more robust!) will be to extend the cart controller on your site with a custom function that that receives a GET url (instead of POST).

Something like

 index.php?route=checkout/cart/addFromUrl&product_id=xx. 

By doing so, you can simply link to this url from any site and all you'll have to change is the product_id in the url. You will not have to use which can be great at situations where you don't have access to the code on the referees side (like an affiliate marketing i.e). It will also be much easier to maintain since your code will be centralized and edited in 1 location vs having to edit it on multiple referring sites.

The actual function will look something like this

 public function addFromUrl() {
    $product_id = isset($this->request->get('product_id')) ? $this->request->get('product_id') : '';
      if ($product_id) {
     //your code (you can use $this->add i.e
     //after adding the product, you will need to redirect to the product page or to the cart page
      }
    }

Hope this helps!
Yani

Upvotes: 2

shadyyx
shadyyx

Reputation: 16055

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

Upvotes: 4

Related Questions