Bitrix: Transferring images of catalog sections from one site to another
Rus
Eng
Битрикс: Перенос изображений разделов каталога с одного сайта на другой

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

Are you developing a new service, making improvements to the existing one and want to be better than your competitors? You have come to the right place. I offer you a comprehensive studio-level website development. From me you can order design, layout, programming, development of non-traditional functionality, implementation of communication between CMS, CRM and Data Analitics, as well as everything else related to sites, except for promotion.

Contact, I will always advise on all questions and help you find the most effective solution for your business. I am engaged in the creation of sites in Novosibirsk and in other regions of Russia, I also work with the CIS countries. You will be satisfied with our cooperation

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

Contact with me
Send message
By submitting the form, you automatically confirm that you have read and accept Privacy policy of site
Sending successful!
Thank you for contacting :) I will contact you as soon as possible
Sending failed
An error occurred while sending the request. Please wait and try again after a while or call my phone number