Jump to content

Need to update category products price using webservice


rvkvino

Recommended Posts

I need to update categories product price using web service so that I have used the below code. This is working only for single product update I couldn't update multiple product at same time.

try {
    $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
   ## Getting category I want
    $opt = array(
        'resource' => 'categories',
        'display' => 'full',
        'filter[id]' => '[3,6]', # we are interested only in one category
        'limit' => '1'
    );
    $xml = $webService->get($opt);
    $ws_cat = $xml->categories->category;
    $products = $ws_cat->associations->products->product;

    ## Gathering products ids to feed the second API call filter parameter
    $productsIds = array();
    foreach ( $products as $p ) {
        $productsIds[] = (int)$p->id;
    }
     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 = 3275; // <-- new price!

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

        // Save product
        $xml = $webService->edit($opt);
    }
} 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();
}

When I run this code I'm getting an error like 

Other error

HTTP XML response is not parsable: array ( 0 => LibXMLError::__set_state(array( 'level' => 3, 'code' => 64, 'column' => 6, 'message' => 'XML declaration allowed only at the start of the document ', 'file' => '', 'line' => 2, )), )

One record getting update well others are getting an issue and showing the above error. Any one help on this to solve this issue. I have tried as static as like in this like https://stackoverflow.com/questions/36883467/how-can-i-update-product-categories-using-prestashop-web-service, this also getting same issue.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...