Bitrix: Transferring images of catalog sections from one site to another
Rus
Eng
Битрикс: Перенос изображений разделов каталога с одного сайта на другой
Order site
By contacting me you will receive a reliable and knowledgeable contractor who will quickly and efficiently implement any task for you and your business.

All English-language materials have been translated fully automatically using the Google service

I describe the solution to the problem of transferring images from one site to another. Provided that in both cases the CODE sections match, and for sections on the source site that do not have an image specified, you need to get it from any product with an image included in the section.

Getting a list of sections with images in json format

  require_once ($ _ SERVER ['DOCUMENT_ROOT']. '/ bitrix / modules / main / include / prolog_before.php');

if (! CModule :: IncludeModule ("catalog")) {die ();}


$ arFilter = [
    'IBLOCK_ID' => 17,
    'IBLOCK_TYPE' => '1c_catalog',
    // 'ACTIVE' => 'Y'
];

$ arSelect = [
    "ID", "NAME", "CODE", "PICTURE", "PREVIEW_PICTURE", "DETAIL_PICTURE", "SECTION_PAGE_URL"
];

// Get a list of information block sections
$ sections = CIBlockSection :: GetTreeList ($ arFilter, $ arSelect);

while ($ ar_dep = $ sections-> GetNext ())
{
    if (! empty ($ ar_dep ['PICTURE']) ||! empty ($ ar_dep ['DETAIL_PICTURE'])) {
// If the section has an image
        $ result [$ ar_dep ['CODE']] = $ ar_dep;

        $ img = $ ar_dep ['PICTURE']? $ ar_dep ['PICTURE']: $ ar_dep ['DETAIL_PICTURE'];

        $ result [$ ar_dep ['CODE']] ['SRC'] = CFile :: GetPath ($ img);

    } else {

        We get the last product in the right section
        $ elementIterator = \ Bitrix \ Iblock \ ElementTable :: getList ([
            'select' => [
                "ID", "NAME", "PREVIEW_PICTURE", "DETAIL_PICTURE", "IBLOCK_SECTION_ID", "CODE",
            ],
            'filter' => [
                '= IBLOCK_ID' => 17,
                '= IBLOCK_SECTION_ID' => $ ar_dep ['ID'],
                '= ACTIVE' => 'Y',
                '! = DETAIL_PICTURE' => '',
            ],
            'order' => ['ID' => 'DESC'], // sorting
            'limit' => 1,
        ]);

        $ elems = $ elementIterator-> fetchAll ();

// If a product is found in the section, then write the src of images
        foreach ($ elems as $ element) {
            $ result [$ ar_dep ['CODE']] = $ ar_dep;

            $ img = $ element ['DETAIL_PICTURE']? $ element ['DETAIL_PICTURE']: $ element ['PREVIEW_PICTURE'];

            $ result [$ ar_dep ['CODE']] ['SRC'] = CFile :: GetPath ($ img);
            $ result [$ ar_dep ['CODE']] ['DETAIL_PAGE_URL'] = $ element ['CODE'];
        }

// If src is not found
        if (! isset ($ result [$ ar_dep ['CODE']] ['SRC'])) {
// We are looking for the first available product included in this section or any subsection of this section
            $ rsResult = CIBlockElement :: GetList (
                array ("SORT" => "ASC"),
                array (
                    "ACTIVE" => "Y",
                    "IBLOCK_ID" => 17,
                    "SECTION_ID" => $ ar_dep ['ID'],
                    "INCLUDE_SUBSECTIONS" => "Y",
                ),
                false,
                $ arNavStartParams = Array ("nTopCount" => 1),
                array ("ID", "NAME", "PREVIEW_PICTURE", "DETAIL_PICTURE", "IBLOCK_SECTION_ID", "CODE")
            );

            while ($ obElement = $ rsResult-> GetNext ()) {
                $ result [$ ar_dep ['CODE']] = $ obElement;

                $ img = $ obElement ['DETAIL_PICTURE']? $ obElement ['DETAIL_PICTURE']: $ obElement ['PREVIEW_PICTURE'];

                $ result [$ ar_dep ['CODE']] ['SRC'] = CFile :: GetPath ($ img);
                $ result [$ ar_dep ['CODE']] ['DETAIL_PAGE_URL'] = $ obElement ['CODE'];

            }
        }

    }

}
// Function for correcting the encoding of the received data
// If json_encode doesn't work
function safe_json_encode ($ value, $ options = 0, $ depth = 512) {
    $ encoded = json_encode ($ value, $ options, $ depth);
    switch (json_last_error ()) {
        case JSON_ERROR_NONE:
            return $ encoded;
        case JSON_ERROR_DEPTH:
            return 'Maximum stack depth exceeded'; // or trigger_error () or throw new Exception ()
        case JSON_ERROR_STATE_MISMATCH:
            return 'Underflow or the modes mismatch'; // or trigger_error () or throw new Exception ()
        case JSON_ERROR_CTRL_CHAR:
            return 'Unexpected control character found';
        case JSON_ERROR_SYNTAX:
            return 'Syntax error, malformed JSON'; // or trigger_error () or throw new Exception ()
        case JSON_ERROR_UTF8:
            $ clean = utf8ize ($ value);
            return safe_json_encode ($ clean, $ options, $ depth);
        default:
            return 'Unknown error'; // or trigger_error () or throw new Exception ()

    }
}

function utf8ize ($ mixed) {
    if (is_array ($ mixed)) {
        foreach ($ mixed as $ key => $ value) {
            $ mixed [$ key] = utf8ize ($ value);
        }
    } else if (is_string ($ mixed)) {
        return utf8_encode ($ mixed);
    }
    return $ mixed;
}


echo safe_json_encode ($ result);  

Updating the found matching code sections

  require_once ($ _ SERVER ['DOCUMENT_ROOT']. '/ bitrix / modules / main / include / prolog_before.php');

$ arTotal = array ();

$ url = 'Path to file c searched';

$ postdata = array ();

$ post = http_build_query ($ postdata);
$ ch = curl_init ($ url);
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($ ch, CURLOPT_POST, 1);
curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post);
$ response = curl_exec ($ ch);
curl_close ($ ch);

// Convert std to array
$ response = json_decode ($ response, True);

$ arTotal ['Sections received'] = count ($ response);

if (! CModule :: IncludeModule ("iblock")) {die ();}

$ IDS = $ KEYS = $ ERRORS = array ();

foreach ($ response as $ key => $ value) {
    $ IDS [$ key] ['SRC'] = $ value ['SRC'];
    array_push ($ KEYS, $ key);
}

$ arFilter = Array ('IBLOCK_TYPE' => '1c_catalog', 'IBLOCK_ID' => 22, 'GLOBAL_ACTIVE' => 'Y', 'CODE' => $ KEYS);
$ db_list = CIBlockSection :: GetList (Array ($ by => $ order), $ arFilter, true);

$ i = $ obnovleno = $ neobnovleno = 0;
while ($ ar_result = $ db_list-> GetNext ()) {

    $ src = 'SERVER_ADRESS'. $ IDS [$ ar_result ['CODE']] ['SRC'];
    $ arFile = CFile :: MakeFileArray ($ src);

    $ arPICTURE = $ arFile;
    $ arPICTURE ["MODULE_ID"] = "iblock";

    $ bs = new CIBlockSection;

    $ arFields = Array (
        "PICTURE" => $ arPICTURE,
    );

    $ res = $ bs-> Update ($ ar_result ['ID'], $ arFields, false, false, true);

    if ($ res) {
        $ obnovleno ++;
    } else {
        $ neobnovleno ++;
        array_push ($ ERRORS, $ res-> LAST_ERROR);
    }

    $ i ++;
}

$ arTotal ['Found sections on new site'] = $ i;
$ arTotal ['Updated sections on the new site'] = $ obnovleno;
$ arTotal ['Error updating sections on the new site'] = $ neobnovleno;
$ arTotal ['List of Errors'] = $ ERRORS;  

Comments

There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment

Send

FEEDBACK

Email me

Please message me to discuss the task. I work remotely and officially (as a self-employed professional); my hourly rate is 1,800 RUB, and I can dedicate 3–4 hours a day to the project. I respond quickly, and the consultation is free.

An error occurred while sending, please try again after a while
Message sent successfully

Phones

+7(993) 007-18-96

Email

info@tichiy.ru

Address

Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site