Prestashop 1.6 API更新产品数量

时间:2015-07-07 07:14:29

标签: xml webservice-client prestashop-1.6

我正在制作一个桥接应用程序。使用Prestashop 1.6多商店购物车同步仓库数据库中的产品数量。

脚本只需要一次更新一个产品数量,我们有产品“ID”和“数量”,但是,我不断收到以下错误:

RETURN HTTP BODY
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code><![CDATA[93]]></code>
<message><![CDATA[parameter "quantity" not writable. Please remove this attribute of this XML]]></message>
</error>
</errors>
</prestashop>

这附带: 其他错误 对PrestaShop Web服务的调用失败并返回HTTP状态400.这意味着:错误请求。

XML错误代码93表描述符中不存在该表。我认为这是通过包含“$ opt = array('resource'=&gt;'products');”

来完成的。

网站上也启用了高级库存控制,此脚本不会对其进行调整(一个也应包含此内容,可能是下一个版本)。

我用来获取XML,更改数量值和更新的代码是:

function test1($id, $quantity){
    $id = (int)$id;
    $quantity = (int)$quantity;

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

        $resources = $xml->children()->children();
    }
    catch (PrestaShopWebserviceException $e)
    {
        // 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<br />'.$e->getMessage();
    }

    if (isset($id) && isset($quantity)){

        $resources->id = $id;
        $resources->quantity = $quantity;
        // Call the web service
        try
        {
            $opt = array('resource' => 'products');
            $opt['putXml'] = $xml->asXML();
            $opt['id'] = $id;
            $xml = $webService->edit($opt);
            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();
        }
    }
}

我用来更新的初始XML是:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<product id="45" xlink:href="https://www.WEBSITE.com/api/products/45"/>
</prestashop>

为更新发送的XML是:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<product id="45" xlink:href="https://www.WEBSITE.com/api/products/45"><id>45</id><quantity>22</quantity></product>
</prestashop>

似乎发送的XML或其他东西缺少表引用或其他东西。

我尝试了几种具有相同结果的方法。有什么想法吗?

由于

3 个答案:

答案 0 :(得分:2)

以下是您创建产品和更改数量的方式

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

        $opt = array('resource' => 'products');

        $opt['postXml'] = $new_xml_content;
        $xml = $webService->add($opt);

        $id = $xml->children()->children()->id;


        return $xml;
    }
    catch (PrestaShopWebserviceException $e)
    {
    }

getIdStockAvailableAndSet($new_xml->product->id);

function set_product_quantity($ProductId, $StokId, $AttributeId){
        $xml = $this->getWebService() -> get(array('url' => PS_SHOP_PATH . '/api/stock_availables?schema=blank'));
        $resources = $xml -> children() -> children();
        $resources->id = $StokId;
        $resources->id_product  = $ProductId;
        $resources->quantity = 10000000;
        $resources->id_shop = 1;
        $resources->out_of_stock=1;
        $resources->depends_on_stock = 0;
        $resources->id_product_attribute=$AttributeId;
        try {
            $opt = array('resource' => 'stock_availables');
            $opt['putXml'] = $xml->asXML();
            $opt['id'] = $StokId ;
            $xml = $this->getWebService()->edit($opt);
        }catch (PrestaShopWebserviceException $ex) {
            echo "<b>Error al setear la cantidad  ->Error : </b>".$ex->getMessage().'<br>';
        }
    }


    function getIdStockAvailableAndSet($ProductId){
        global $webService;
        $opt['resource'] = 'products';
        $opt['id'] = $ProductId;
        $xml = $webService->get($opt);
        foreach ($xml->product->associations->stock_availables->stock_available as $item) {
            //echo "ID: ".$item->id."<br>";
            //echo "Id Attribute: ".$item->id_product_attribute."<br>";
            set_product_quantity($ProductId, $item->id, $item->id_product_attribute);
        }
    }

答案 1 :(得分:1)

如你所说,字段&#34;数量&#34;在产品中写保护,和 在架构&#34; stock_available&#34;你有一个可以写的数量字段。实际上,这是你应该写的领域。

你不必触摸数据库;您可以直接通过WS更新库存。

架构&#34; stock_availables&#34;存储您商店中所有产品组合的ID。这就是架构的含义:

<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
  <stock_available>
  <id_product required="true" format="isUnsignedId"/>
  <id_product_attribute required="true" format="isUnsignedId"/>
  <id_shop format="isUnsignedId"/>
  <id_shop_group format="isUnsignedId"/>
  <quantity required="true" format="isInt"/>
  <depends_on_stock required="true" format="isBool"/>
  <out_of_stock required="true" format="isInt"/>
 </stock_available>
</prestashop>

id_product_attribute是指产品的一种组合。如果此属性为零,则表示产品没有任何组合。正如我所说,这个模式存储了您拥有的产品的所有组合(所有这些组合),因此如果产品有多个组合,您将不得不寻找它。

数量字段有3种模式:产品,组合和库存_可用性。只有最后一个是&#34;使用&#34;。第一个是写保护,第二个是我可以调查的,后面的商店没有使用它。

我希望这可以帮助你,并为我糟糕的英语道歉。

答案 2 :(得分:1)

我想分享我的产品数量更新解决方案(希望能帮助其他人)。它可能不是最好的解决方案,但是hek,它的工作原理(对我而言)。

正如Jarlaxxe所说:编辑产品数量的方法是:api / stock_availables。因此,为了确定stock_availables ID,首先需要获取产品信息并从那里检索stock_availables ID,然后放入。

$webService = new PrestaShopWebservice('http://yoursite.com', 'LADEDILADEDA-BTW-YOUR-API-KEY-HERE', false);
    updateStock(productnumber, quantity);

    function updateStock($productID, $quantity){
        //First we got the stock_availables ID from the Product ID
        try
        {
            $opt = array('resource' => 'products');
            $opt['id'] = $productID;
            $xml = $webService->get($opt);
            $resources = $xml->children()->children();
            $stockID = $resources->associations->stock_availables->stock_availables;

            foreach($stockID as $stock){                
                $stockAVailableID = $stock->id;
            }

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

        //Second we get all the stockAvailables resources
        try
        {
            $opt = array('resource' => 'stock_availables');
                $opt['id'] = $stockAVailableID;
            $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();
        }


        //At last we update the quantity with the given value manupulated and all other values original
        foreach ($resources as $nodeKey => $node)
        {
            if($nodeKey == 'quantity'){
                unset($node);
                $node = $quantity;
            }
            $resources->$nodeKey = $node;
        }
        try
        {
            $opt = array('resource' => 'stock_availables');
            $opt['putXml'] = $xml->asXML();
            $opt['id'] = $stockAVailableID;
            $xml = $webService->edit($opt);
            echo "Successfully updated.";
        }
        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();
        }
    }