PrestaShop WebService - 各种产品的价格更新

时间:2017-12-19 07:44:15

标签: php mysql web-services prestashop prestashop-1.6

我有这个用WebService更新一个产品的PrestaShop价格。

    <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表中的所有产品呢?

我将所有产品都放在MySQL表中,ID Key为Source_ID。

感谢任何帮助!

此致

1 个答案:

答案 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>

希望这有帮助!