Jump to content

Example of Jquery/JavaScript API request which will edit something in order


hakeryk2

Recommended Posts

Hello community,

 

I try to edit shipping number in order using prestashop API and jQuery and Javascript.

I read all of the docs about API and I can create GET requests to receive any information that I want but I still can't put anything by using jQuery.

Does anyone has any example how to edit something in order or whatever in prestashop using jquery/javascript?

Docs are saying:

 

 

 

Adding and editing a resource

The http://example.com/api/customers/1 example is not only available using the HTTP GET method, but also using POST, PUT, DELETE, HEAD.

To add a customer (or any other resource, for that matter), you simply need to GET the XML blank data for the resource (/api/customer?schema=blank), fill it with your changes, and POST the whole XML file to the /api/customers/ URL again. PrestaShop will take care of adding everything in the database, and will return an XML file indicating that the operation has been successful, along with the ID of the newly created customer.

To edit an existing resource: GET the full XML file for the resource you want to change (/api/customers/7), edit its content as needed, then PUT the whole XML file back to the same URL again.

 

This is just to global and overall it is not helping with nothing.

How to edit something via API using just browser calls?

Link to comment
Share on other sites

Eh, community, this is my 4th question about prestashop API and I didn't get any answer but after long run I figured it out how to update order informations (and any other) through ajax/jquery/javascript.

 

var api_url = 'https://yourdomain/api/';
var api_key = 'Y0uRG3n3r4T3D4P1K3Y'; // with permissions to put info. 

$.ajax({
                url: api_url + 'orders/' + order_id,
                type: "GET", 
                dataType: "xml",
                username: apiKey,
                password: "",
                processData: false,
                contentType: "xml",
                success: function (data) {

                    // Here You can change or update desired fields
                    $xml = $(data),
                    $xml_shipping_number = $xml.find( "shipping_number" ); // what You want to change
                    /* Add tracking number */
                    $xml_shipping_number.text("123456"); // your shipping number


                    // now let's send this back to API
                    $.ajax({
                        url: api_url + 'orders/' + order_id,
                        type: "PUT", 
                        dataType: "xml",
                        username: apiKey,
                        password: "",
                        data: data,
                        processData: false,
                        contentType: "xml",
                        success: function () {
                            console.log('Success! Added Shipping number')
                        },
                        error: function (xhr, ajaxOptions, thrownError) { //Add these parameters to display the required response
                            alert(xhr.status);
                            alert(xhr.responseText);
                        },
                    });


                },
                error: function (xhr, ajaxOptions, thrownError) { //Add these parameters to display the required response
                    alert(xhr.status);
                    alert(xhr.responseText);
                },
            });

In my case I was editing shipping number but You can edit whatever You want.

I am just leaving this here because probably a lot people are looking for these.

BE Careful to keep those API keys to yourself and run your browser in cross-origin enabled by disabling web-security

You can do it by running Chrome in Run -> chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security
 

Edited by hakeryk2 (see edit history)
  • Like 4
  • Thanks 1
Link to comment
Share on other sites

  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...