PrestaShop:通过web服务更新客户信息


PrestaShop: Update customer through web-service

我想使用webservice更新PrestaShop中的客户。我根据他的电子邮件和公司名称搜索客户,然后更新新的详细信息。但是当我使用过滤器获取客户详细信息然后更新时,我收到以下错误,我不知道如何解决这个问题。

修改资源时需要90 - id错误

$email=$_POST['email'];
$oldEmail=$_POST['oldEmail'];
$company=$_POST['company'];
try
{
    $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
    $opt = array(
        'resource' => 'customers',
        'display' => 'full',
        'filter[email]'=>'e.siona@example.gr',//['.$oldEmail.']',
        'filter[company]'=>'euro'//['.$company.']'
    );
    $xml = $webService->get($opt);
    // Retrieve resource elements in a variable (table)
    $resources = $xml->children()->children();
    //$opt['id'] = 17;
    //echo $resources->id;
}
catch (PrestaShopWebserviceException $e){
    // Here we are dealing with errors
    $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';
}
$resources->email = 'e.siona43@example.gr';
// Resource definition
$opt = array('resource' => 'customers');
//XML file definition
$opt['putXml'] = $xml->asXML();
$opt['id'] = 'How can I put here the id retrieved from previous call?'; 
// Calling asXML() returns a string corresponding to the file
$xml = $webService->edit($opt);
// Second : We update the data and send it to the web service

官方github示例出错

试试这段代码的更正版本:

$email=$_POST['email'];
$oldEmail=$_POST['oldEmail'];
$company=$_POST['company'];
try
{
    $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
    // Filters must be an array (I'll suggest this method)
    $filter = array(
        'email' => $oldEmail,
        'company' => 'ACME'
    );
    // I guess it's useless to use company because the e-mail it's a KEY in the database, so the correspondence 'should' be univocal.
    $opt = array(
        'resource' => 'customers',
        'display' => 'full',
        'filter' => $filter
    );
    $xml = $webService->get($opt);
    // Retrieve resource elements in a variable (table)
    $resources = $xml->children()->children();
}
catch (PrestaShopWebserviceException $e){
    // Here we are dealing with errors
    $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';
}
// Here we retrieve the ID
$customer_id = $resources->customer->id; 
// Here we update the e-mail
$resources->customer->email = $email; // E.g. 'e.siona43@example.gr';
// And call the web service
try
{
    $opt = array('resource' => 'customers');
    $opt['putXml'] = $xml->children()->asXML(); // This is correct if you have one customer in the XML. The GitHub examples it's incomplete and you get the error 90.
    $opt['id'] = $customer_id;
    $xml = $webService->edit($opt);
    // if WebService don't throw an exception the action worked well and we don't show the following message
    echo "Successfully updated.";
}
catch (PrestaShopWebserviceException $ex)
{
    // Here we are dealing with errors
    $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();
}

希望这对你有帮助