Prestashop 1.6 API Update Product Quantity

IeM picture IeM · Jul 7, 2015 · Viewed 9.4k times · Source

I'm making a bridging app. to sync product quantities from a warehouse DB with a Prestashop 1.6 Multi-Store cart.

The script just needs to update one product quantity at a time and we have the product "ID" and the "quantity", however, I keep getting the following error returned:

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>

This comes with: Other error This call to PrestaShop Web Services failed and returned an HTTP status of 400. That means: Bad Request.

XML error code 93 is The table is not present in the descriptor. I thought this was done by including "$opt = array('resource' => 'products');"

Advanced Stock control is also enabled on the site and this script does not make adjustments with it(One should also include this, maybe next version).

The code I'm using to get the XML, change the quantity value, and update is:

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

The initial XML returned that I use to update is:

<?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>

The XML that was sent for the Update is:

<?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>

It seems that the sent XML or something is missing the table reference or something.

I've tried a couple ways all with the same result. Any ideas?

Thanks

Answer

Elia Weiss picture Elia Weiss · Feb 13, 2016

Here is how u create a product and change the quantity

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