2017-12-19 39 views
0

WebService를 사용하여 하나의 제품에 대한 PrestaShop 가격을 업데이트했습니다.PrestaShop WebService - 다양한 제품의 가격 업데이트

<html><head><title>CRUD Data Transfer - Update example</title></head><body> 
<?php 
// Here we define constants /!\ You need to replace this parameters 
define('DEBUG', true); 
define('PS_SHOP_PATH', 'https://my.domain.com'); 
define('ID_PRODUCT', 1); 
define('PS_WS_AUTH_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'); 
require_once('./PSWebServiceLibrary.php'); 
@ini_set('display_errors', 'on'); 
try 
{ 
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); 
$opt = array('resource' => 'products'); 
$opt['id']=ID_PRODUCT; 
$xml = $webService->get($opt); 
echo "Successfully recived data."; 
    /* List of nodes that can't modify 
    * 
    * - "manufacturer_name" 
    * - "position_in_category" 
    * - "quantity" 
    * - "type" 
    */ 
    unset($xml->children()->children()->manufacturer_name); 
    unset($xml->children()->children()->position_in_category); 
    unset($xml->children()->children()->quantity); 
    unset($xml->children()->children()->type); 
    $xml->children()->children()->price = 111.0; // <-- new price! 
//Load new data to query generator 
$opt['putXml']=$xml->asXML(); 
$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(); 
} 

?> 
</body></html> 

동일한 방법을 사용할 수 있지만 MySQL 테이블의 모든 제품에 대해 어떻게 할 수 있습니까?

ID 키가 Source_ID 인 MySQL 테이블에 모든 제품이 있습니다.

감사합니다.

감사

+0

없음 ?? : –

답변

0

당신은 각 제품의 제품 데이터를 검색 그 가격의 업데이트 한 다음 저장해야합니다. 다음과 같이 시도하십시오.

<html><head><title>CRUD Data Transfer - Update example</title></head><body> 
<?php 
// Here we define constants /!\ You need to replace this parameters 
define('DEBUG', true); 
define('PS_SHOP_PATH', 'http://my.domain.com'); 
define('PS_WS_AUTH_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXX'); 
require_once('PSWebServiceLibrary.php'); 
@ini_set('display_errors', 'on'); 

try { 
    $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); 

    // Get products from your database via a query 
    $products = array(array('id' => 1, 'price' => 1.5,), array('id' => 2, 'price' => 2.95)); 

    // Loop through products 
    foreach ($products as $product) { 

     $opt = array('resource' => 'products', 'id' => $product['id']); 

     // Call webservice to get product data 
     $xml = $webService->get($opt); 

     // Remove nodes that can't be modified 
     unset($xml->children()->children()->manufacturer_name); 
     unset($xml->children()->children()->position_in_category); 
     unset($xml->children()->children()->quantity); 
     unset($xml->children()->children()->type); 

     // Update product data 
     $xml->children()->children()->price = $product['price']; // <-- new price! 

     // Load new data to query generator   
     $opt['putXml'] = $xml->asXML(); 

     // Save product 
     $xml = $webService->edit($opt); 
     // if WebService don't throw an exception the action worked well and we don't show the following message 
    } 
} 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(); 
} 
?> 
</body></html> 

희망이 있습니다.

+0

도와 주셔서 감사합니다 잠시 동안 고객님과 시도했지만 첫 번째 제품 만 업데이트합니다 : 'id'=> 1, 'price'=> 1.5 다른 하나는 업데이트되지 않습니다. :( –

+0

아, 슬픈 일입니다 .--(업데이트에 오류가 있습니까? 실제로 ID 2가있는 제품이 상점에 있습니까? – ArisS

+0

안녕하십니까 .ArisS 아니요, end : 기타 오류 PrestaShop Web Services에 대한이 호출이 실패하고 HTTP 상태 500을 반환했습니다. 즉, 내부 서버 오류 다른 질문이 있습니다. 예, 실제로 제 상점에서 ID가 1,2 인 3 개의 제품이 있습니다. 3. –