Message-ID: <1734206880.379194.1711699519572.JavaMail.root@confluence-doc2-production> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_379193_1799395901.1711699519567" ------=_Part_379193_1799395901.1711699519567 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Objective: A Web application to list and create a new c=
ustomer.
Difficulty: **
Copy the file list_the_clients.php
from Section 3.3 to a fi=
le named C-CRUD.php
at the root of your Web server.
The addition of resource can be likened to an upgrade from an empty elem= ent.
But how do we retrieve an XML formatted as an empty client?
In the web service, there is a method to retrieve an empty XML. It is ac=
cessible via a URL formatted as follows: http:=
//example.com/api/(resource name)?schema=3Dblank
It is possible to replace the parameter scheme value "blank" with "synop= sis" in order to gain more information about the resource fields.
As we saw in Section 3.3 (List customers) it is possible make an array o= f parameters for "get ", "resource," and "id." It is also possible to speci= fy only one URL this way:
$xml =3D= $webService->get(array('url' =3D> 'http://example.com/api/customers?= schema=3Dblank'));=20
Here, we get the entire XML variable from an empty client.
<pres= tashop> <customer> <id_default_group/> etc...Beginning of the XML file retrieved:=20
We can then, thanks to the many fields we have, create an associated for= m.
$resourc= es =3D $xml->children()->children();=20
Path of all fields and part of the dynamic creation of form fields in a = table
foreach = ($resources as $key =3D> $resource) { =09echo '<tr><th>' . $key . '</th><td>';=20 =09echo '<input type=3D"text" name=3D"' . $key . '" value=3D""/>'; =09echo '</td></tr>'; }=20
Once the data is passed in POST, we combined the data sent with the blan= k XML file, which is the same technique used for updating data.
foreach = ($resources as $nodeKey =3D> $node) { =09$resources->$nodeKey =3D $_POST[$nodeKey]; }=20
Calling the web service is similar to what we have seen previously:
$opt =3D= array('resource' =3D> 'customers'); $opt['postXml'] =3D $xml->asXML(); $xml =3D $webService->add($opt);=20
Now create a script that adds a client. Remember that some fields are ma= ndatory, so do not forget to fill them out.
If you have trouble, look at the code the 3-Create.php
samp=
le file.
When a client is created from within PrestaShop's administration interfa= ce, a confirmation e-mail is sent to the client. This cannot be done direct= ly with the webservice: there is no way to trigger the sending of that conf= irmation e-mail.
However, you can create an override file for the Customer
c=
lass and override the addWs()
method. This method is similar t=
o ObjectModel::add()
but is only called from the webservice. Y=
ou can find examples of its use in the Product
and Order=
classes.