PrestaShop:通过网络服务更新客户

时间:2016-09-13 20:47:11

标签: php web-services prestashop prestashop-1.6

我想使用网络服务更新 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

1 个答案:

答案 0 :(得分:3)

official 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();
}

希望这会有所帮助:)