Jump to content

Update product images position and not image via Webservice (API)


Tauno

Recommended Posts

Hi, 

Maybe I am stupid, but I cannot find a way to update product images positions (the order how they show up at a product details for user visiting the shop) via Webservice withoug actually deleting the images and adding new ones in another order. Can it be done at all?

There is "position" colum in the "ps_image" table in the database, but for some unbelievable reason I have not managed to figure out how to change that value via Webservice (am using Prestashop 1.7.8.2)?

It is a very bad idea to delete old photos and add new ones every time the user just wants to reposition them in my oppinion, but for some reason I cannot find a way to do it via products associatons and resaving the product or similar.

Can anyone help with a suggestion?

Thank you so much for help and support!

Best regards,

Tauno

Edited by Tauno (see edit history)
Link to comment
Share on other sites

  • Tauno changed the title to Update product images position and not image via Webservice (API)

Hi, Constantino, 

It is sure possible to update product positions in any backoffice admin tool, but my question is really how or if it is possible to do it using Webservice API as we are linking our manufacturing management system directly with Prestashop and are not using any separate backoffice administration tools at all. :)

Thanks for anyone for help as right now we have it implemented so that all product images are deleted and reauploaded every time a user updates the order or any of the product photos. This is really stupid and wasteful and can cause UI issues to shop users as well during such updates, so any help is highly appreciated to implement only the position update of the files. :)

 

Thank you and have a nice weekend!

Link to comment
Share on other sites

People desperately trying to sell their things are the worst. @Constantino

The only way to deal with images in API appears to be via POST or PUT and both require complete image data. I.e. you can't PUT a product's XML with image order different from the current one, it will get ignored. The only thing you can change is the default image. So it's either deleting and re-POSTing (or PUTting over the old ones without deleting) or just modifying the table directly - the latter doesn't sound like a bad choice at all, but will require additional programming and server access.

  • Like 1
Link to comment
Share on other sites

Hi, thanks for the comment, JustJohn.

In that case repost it is! :D 

We want the instances be as independent and flexibly reconfigurable as possible and for our small application the load that could come from reposting the images will not be too heavy. Just hoped I was missing something and there was some faster and leaner way for rearranging photos. :)

Thanks once again for help and support. :)

Best regards,

Tauno

Link to comment
Share on other sites

Hi.

You can avoid webservice altogether, because it is quite complex.
It is very easy to make your own script including a security token and send it data from an external program in XML format.
There should be some identifier in this XML to use the sort by your external program.

What all would need to be done?

1. when importing images, have an identifier in the XML file
2. add a column with this different identifier to the ps_image table
3. override Image.php and add a column with a new identifier to the definition
4. create your own PHP script to update the image position

Alternatively, program a small module that would take care of everything.

It sounds very complicated, but it's not.

Edited by 4you.software (see edit history)
Link to comment
Share on other sites

Sample for override Image.php and add identify external_id:

./override/classes/Image.php

<?php

class Image extends ImageCore
{
    public $external_id;
    
    public function __construct($id = null, $idLang = null)
    {
        self::$definition['fields']['external_id'] = array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId');
        parent::__construct($id, $idLang);
    }
}

 

Sample for update webservice image:

./classes/webservice/WebserviceSpecificManagementImages.php

find and change to your position:

$image->position = Image::getHighestPosition($product->id) + 1;

and find and change to your cover:

if (!Image::getCover((int) $product->id)) {
                            $image->cover = 1;
                        } else {
                            $image->cover = 0;
                        }

 

Sample custom PHP script to update images:

./prestashop root/changeImagePosition.php

<?php

header("Access-Control-Allow-Origin: *");

include('./config/config.inc.php');
include('./init.php');

$token = 'my_custom_token';
$getToken = 'posted_custom_token';

if ($token == $getToken){
    $getXml = $_POST['xml'];
    /*
    <xml>
        <images>
            <image>
                <id>1</id>// external_id
                <position>1</position>
                <cover>1</cover>
            </image>
            <image>
                <id>2</id>// external_id
                <position>2</position>
                <cover>0</cover>
            </image>
        </images>
        <images>
            <image>
                <id>3</id> // external_id
                <position>1</position>
                <cover>0</cover>
            </image>
            <image>
                <id>4</id> // external_id
                <position>2</position>
                <cover>1</cover>
            </image>
        </images>
    */

            
    $xml = simplexml_load_string(file_get_contents($getXml), 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);

    foreach ($xml->images as $image) {
        foreach ($image->image as $data) {
            Db::getInstance()->update('image', 
                array(
                    'position' => $data['position'],
                    'cover' => $data['cover']
                ), 'external_id='.$data['id']
            );
        }    
    }
}

 

Edited by 4you.software (see edit history)
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...