Jump to content

Update customer via webservice.


abbest

Recommended Posts

So i've been trying to get it work for the past few hours but still having problems.

 

<?php

define('DEBUG', false);											
define('PS_SHOP_PATH', 'http://localhost/loko');		
define('PS_WS_AUTH_KEY', '7NC39W7K8SK54UUR48C2ATF99F9LAIFB');	
require_once('./PSWebServiceLibrary.php');

try
{
	$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
	$opt = array('resource' => 'customers');
	if (isset($_GET['id']))
		$opt['id'] = $_GET['id'];
	$xml = $webService->get($opt);

	$resources = $xml->children()->children();
}
catch (PrestaShopWebserviceException $e)
{
	$trace = $e->getTrace();
	if ($trace[0]['args'][0] == 404) echo 'Bad ID';
	else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
	else echo 'Other error<br />'.$e->getMessage();
}

if (isset($_GET['id']) && isset($_POST['id']))
{
	foreach ($resources as $nodeKey => $node)
	{
		$resources->$nodeKey = $_POST[$nodeKey];
	}
	try
	{
		$opt = array('resource' => 'customers');
		if ($_GET['Update'] == 'Update')
		{
			$opt['putXml'] = $xml->asXML();
			$xml = $webService->edit($opt);
			echo "Successfully added.";
		}
	}
	catch (PrestaShopWebserviceException $ex)
	{
		$trace = $ex->getTrace();
		if ($trace[0]['args'][0] == 404) echo 'Bad ID';
		else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
		else echo 'Other error<br />'.$ex->getMessage();
	}
}

if (isset($_GET['id']))
	echo '<form method="POST" action="?id='.$_GET['id'].'">';
?>

<br>
<!-- Showing all customers in table -->
<table
  class="table table-striped table-bordered table-hover"
  data-provide="datatable"
  data-info="true"
>

      <?php
          if(isset($resources)) {
            if(!isset($_GET['id'])) {
              ?>
              <thead>
                <tr>
                  <th data-filterable="true">Customer's Id</th>
                  <th data-filterable="true">Firstname</th>
                  <th data-filterable="true">Lastname</th>
                  <th data-filterable="true">Email</th>
                  <th data-filterable="false" class="hidden-xs hidden-sm">Update</th>
                </tr>
              </thead>
              <tbody>
              <?php
              foreach ($resources as $resource ) {
                $opt['id'] = $resource->attributes();
                $xml = $webService->get($opt);
                $r = $xml->children()->children();
                echo '<tr>';
                echo '<td>'.$r->id.'</td>';
                echo '<td>'.$r->firstname.'</td>';
                echo '<td>'.$r->lastname.'</td>';
                echo '<td>'.$r->email.'</td>';
                echo '<td><a href="?id='.$resource->attributes().'">Update</a></td>';
                echo '</tr>';
              }
            } else {
              foreach ($resources as $key => $resource) {
                echo '<tr>';
                echo '<th>'.$key.'</th><td>';
                echo '<input type="text" id="name" name="'.$key.'" value="'.$resource.'" class="form-control" data-required="true" >';
                echo '</td></tr>';
              }
            }
          }
      ?>


    </tbody>
  </table>
<!-- Showing all customers in table -->

<?php

if (isset($_GET['id']))
	echo '<input type="submit" class="btn btn-success" value="Update"></form>';





?>

If anyone can help me out please 

Edited by abbest (see edit history)
Link to comment
Share on other sites

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...