The initiator of any exchange is 1C, the site does not upload anything to 1C, the site only accepts requests and returns the result. By default, 1C accesses the script /bitrix/admin/1c_exchange.php. This path is specified in the exchange settings in 1C itself. Out of the box, a file is included in this file /bitrix/modules/sale/admin/1c_exchange.php. If you open the file, you will see the connection of several components there, depending on $_GET["type"] .In general, we are interested in two of them:
bitrix:catalog.import.1c – import the catalog.
bitrix:sale.export.1c – export orders.
Importing goods from 1C
The import begins with authorization, and a request is sent to /bitrix/admin/1c_exchange.php?type=catalog&mode=checkauth with the transfer of the login and password of the site user, which were specified in the exchange settings. After authorization, the site issues a session ID.
Next comes the initialization of the exchange, URL /bitrix/admin/1c_exchange.php?type=catalog&mode=init&sessid=ID_Sessions. At this stage, the exchange data array $_SESSION["BX_CML2_IMPORT"] is initialized in the session and the site provides the exchange parameters: the ability to use zip, file size limit, etc. Also at this stage, the folder for the upload files is cleared. Most likely, it will be the /upload/1c_catalog/ folder. For debugging, you can enable saving old exchange files, then the old data will be moved to separate folders during the new exchange.: 1c_catalog0, 1c_catalog1, etc. To save data, you need to declare a constant in dbconn.php:
// Do not forget to remove this line after debugging to avoid server overflow define("BX_CATALOG_IMPORT_1C_PRESERVE", true);
After that, 1C starts preparing the data and then sends it by POST request to the site in parts, URL: /bitrix/admin/1c_exchange.php?type=catalog&mode=file&filename=FILE_NAME.zip&sessid=ID_Sessions. The request continues until the files are fully transferred.
1C transmits xml files with product data and offers, as well as images, to the site. If the site has allowed the use of zip, then 1C transfers all files in one archive. Then the next step starts with unpacking it.
XML files may be involved in the upload.:
- import.xml - products, sections, price types, warehouses, product properties and units of measurement;
- offers.xml - trade offers of goods and their properties;
- prices.xml - prices of trade offers (in new versions);
- rests.xml - remaining sales offers (in new versions);
- references.xml - user reference books (highload information blocks, in new versions).
Learn more about the exchange files
The images are uploaded to the /upload/1c_catalog/import_files/ folder.
Next, the file processing and data import itself begin, 1C repeatedly accesses the URL. /bitrix/admin/1c_exchange.php?type=catalog&mode=import&filename=ИМЯ_ФАЙЛА_ВЫГРУЗКИ.xml, where is the UPLOAD FILE_NAME - import.xml , offers.xml and so on. The import is completed when the site returns the word "success". The CIBlockXMLFile (/) classes are responsible for processing the upload file.bitrix/modules/iblock/classes/mysql/cml2.php ) and CIBlockCMLImport (/bitrix/modules/iblock/classes/general/cml2.php ).
The import consists of several steps:
-
Clearing the temporary table (table b_xml_tree)
CIBlockXMLFile method::DropTemporaryTables(). The usual drop of the b_xml_tree table.
-
Creating a temporary table
CIBlockXMLFile method::CreateTemporaryTables(). The b_xml_tree table is created here. By the way, you can set the storage engine of this table, as well as all those created by Bitrix, by defining the MYSQL_TABLE_TYPE constant.
-
Reading a file into a temporary table
The CIBlockXMLFile::ReadXMLToDatabase() method.
-
Indexing a temporary table
The CIBlockXMLFile::IndexTemporaryTables() method.
-
Importing metadata
The CIBlockCMLImport::ImportMetaData method.
-
Importing partitions
The CIBlockCMLImport::ImportSections method.
-
Deactivating partitions and recalculating the left and right borders for partitions
CIBlockCMLImport Methods::DeactivateSections and CIBlockCMLImport::SectionsResort.
-
Importing elements
The longest process, at this stage new products are added and updated (if uploaded import.xml ) or prices and balances are updated (if uploaded offers.xml ). This uses the CIBlockCMLImport::ImportElements method, which calls CIBlockCMLImport::ImportElement for items or CIBlockCMLImport::ImportElementPrices for balances/prices.
-
Deactivating the elements
CIBlockCMLImport Methods::DeactivateSections and CIBlockCMLImport::SectionsResort.
-
success
The file import is completed, the site will respond with the word "success", 1C considers the exchange successful. If necessary, the import of the next file begins.
Some steps can be performed in several requests, in chunks of several seconds (this time is set by the "Interval of one step in seconds" parameters in the 1C integration settings in the Bitrix admin panel).
Modification
At step 9, i.e. after processing the upload file (NB: one file, for example, products or offers), the OnSuccessCatalogImport1C event of the catalog module is triggered. The handler arguments are the parameters of the exchange component and the path to the upload file.
You can also use regular events to change the exchange process, such as OnBeforeIBlockElementUpdate or OnBeforeProductAdd. In the handler, to determine that the event was triggered during the exchange with 1C, I use such a crutch condition:
if (isset($_GET['type'], $_GET['mode']) && $_GET['type'] === 'catalog' && $_GET['mode'] === 'import') {
// ...
}
Well, if nothing can be solved by events, you can always completely change the exchange process by copying and modifying the file. /bitrix/admin/1c_exchange.php, component bitrix:catalog.import.1c And the class CIBlockCMLImport.
Disabling request source verification
As in the case of unloading goods, when importing goods, there is a verification of the request source. You can disable it with the following code:
// old school:
COption::SetOptionString("catalog", "DEFAULT_SKIP_SOURCE_CHECK", "Y");
// d7 style:
\Bitrix\Main\Config\Option::set("catalog", "DEFAULT_SKIP_SOURCE_CHECK", "Y");
Exporting orders to 1C
The exchange of orders begins with authorization using the username and password that you specified in the exchange settings. After authorization, the site issues the session ID, URL: /bitrix/admin/1c_exchange.php?type=sale&mode=checkauth.
After receiving the session ID, the exchange is initialized, the URL is: /bitrix/admin/1c_exchange.php?type=sale&mode=init&sessid=ID_Sessions. At this stage, the site returns the exchange settings from the site.
The URL is requested to get the list of orders. /bitrix/admin/1c_exchange.php?type=sale&mode=query&sessid=ID_Sessions. Orders with the fields EXTERNAL_ORDER = "N", UPDATED_1C = "N" and updated after the last successful upload to 1C are uploaded (this is checked by time from the "last_export_time_committed_/bitrix/admin/1c_excha" property of the sale module).
Flag EXTERNAL_ORDER indicates that the order was created in 1C and does not need to be uploaded to 1C.
The UPDATED_1C flag is set to "Y" after successful import from 1C on the site, and such orders do not participate in further exports until they are updated on the site. During order updates on the website, UPDATED_1C is set to "N".
After processing the orders, 1C requests the URL to complete the exchange. /bitrix/admin/1c_exchange.php?type=sale&mode=success&sessid=ID_Sessions. At the same time, the "last_export_time_committed_/bitrix/admin/1c_excha" property of the sale module records the time of the request for the list of orders (NOT the current time, because orders could have been added from the moment the orders were requested until they were processed and they will be uploaded next from the site).
Modification
A file is involved in exporting orders. /bitrix/admin/1c_exchange.php, component bitrix:sale.export.1c And the class CSaleExport. There are no events here, so to modify this process, you need to replace the component with your own, create a CSaleExport inheritor class and replace the calls in the component with it. In recent versions of Bitrix, this class has become relatively readable, in the old ones there was just a sheet of php and "xml".
Disabling request source verification
Since version 15.5, an additional check has been added and if called simply /bitrix/admin/1c_exchange.php?type=sale&mode=query, then we get the error "failure Error verifying the request source. Update the exchange module." If you do not want to mess with the sessid, you can disable the verification of the request source. To do this, do:
// old school:
COption::SetOptionString("sale", "secure_1c_exchange", "N");
// d7 style:
\Bitrix\Main\Config\Option::set("sale", "secure_1c_exchange", "N");
We also want to share important links, cases, and answers from Bitrix developers related to the specifics of 1C-Bitrix exchange.:
-
The official Bitrix Forum, or rather, a topic dedicated to the Exchange of 1C
-
Documentation on the Exchange of 1C from Bitrix, available at the link
-
Analysis typical errors
-
Answers Frequently asked questions