【问题标题】:Prestashop 1.6 API Update Product QuantityPrestashop 1.6 API 更新产品数量
【发布时间】:2015-07-07 07:14:29
【问题描述】:

我正在制作一个桥接应用程序。将仓库数据库中的产品数量与 Prestashop 1.6 Multi-Store 购物车同步。

脚本只需要一次更新一个产品数量,我们有产品“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' => '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 或其他东西缺少表引用或其他东西。

我尝试了几种方法,结果都一样。有什么想法吗?

谢谢

【问题讨论】:

  • 更新: 我发现 Product->quantity 是写保护的,并且数量需要在其他地方更新,例如 Stock_available->quantity 我可以做。但是,当我直接在数据库中更新这两个值作为测试时,它们不会显示在后台。因此,更新库存并将其显示在后台会更加​​复杂。任何人都知道究竟要更新什么来改变库存水平,如何更新写保护的产品->数量,以及如何让更新显示在后台?

标签: xml webservice-client prestashop-1.6


【解决方案1】:

正如您所说,产品中的“数量”字段是写保护的,并且 在模式“stock_available”中,您有一个可以编写的数量字段。确实,这是你应该写的字段。

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

架构“stock_availables”存储您商店中所有产品组合的 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 种模式中:产品、组合和 stock_availables。只有最后一个是“使用”的。第一个是写保护的,而第二个,我可以调查,不被后台存储使用。

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

【讨论】:

  • 感谢 Jarlaxxe,你的英语做得很好。目前我不得不继续处理其他问题,但我希望尽可能采纳您的建议。
  • 请尽量写得简单点,我不明白该怎么做
【解决方案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);
        }
    }

【讨论】:

    【解决方案3】:

    我想分享我的产品数量更新解决方案(希望对其他人有所帮助)。它可能不是最好的解决方案,但它确实有效(对我来说)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-12
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多