From 895b19fd37b0dbf7dbb79166e2cd183466baf29a Mon Sep 17 00:00:00 2001 From: Edoardo Gusmaroli Date: Thu, 17 Oct 2024 12:16:26 +0200 Subject: [PATCH] add update (PATCH) --- PSWebServiceLibrary.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/PSWebServiceLibrary.php b/PSWebServiceLibrary.php index ba67fc1..d0bf1f3 100644 --- a/PSWebServiceLibrary.php +++ b/PSWebServiceLibrary.php @@ -469,6 +469,43 @@ public function edit($options) return $this->parseXML($request['response']); } + /** + * Update (PATCH) a resource + *

Unique parameter must take :

+ * 'resource' => Resource name ,
+ * 'id' => ID of a resource you want to edit,
+ * 'patchXml' => Modified XML string of a resource

+ * Examples are given in the tutorial

+ * + * @param array $options Array representing resource to edit. + * + * @return SimpleXMLElement + * @throws PrestaShopWebserviceException + */ + public function update($options) + { + $xml = ''; + if (isset($options['url'])) { + $url = $options['url']; + } elseif ((isset($options['resource'], $options['id']) || isset($options['url'])) && $options['patchXml']) { + $url = (isset($options['url']) ? $options['url'] : + $this->url . '/api/' . $options['resource'] . '/' . $options['id']); + $xml = $options['patchXml']; + if (isset($options['id_shop'])) { + $url .= '&id_shop=' . $options['id_shop']; + } + if (isset($options['id_group_shop'])) { + $url .= '&id_group_shop=' . $options['id_group_shop']; + } + } else { + throw new PrestaShopWebserviceException('Bad parameters given'); + } + + $request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'PATCH', CURLOPT_POSTFIELDS => $xml)); + $this->checkStatusCode($request);// check the response validity + return $this->parseXML($request['response']); + } + /** * Delete (DELETE) a resource. * Unique parameter must take :