Jump to content

Edit History

ps8modules

ps8modules

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

 

ps8modules

ps8modules

Sample for override Image.php and add identify external_id:

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

 

And sample custom PHP script to update images:

<?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']
            );
        }    
    }
}

 

×
×
  • Create New...