Views: 9981
Last Modified: 10.10.2024

Let us consider an example of the function which copies the field values of infoblock elements ($_FROM_FIELD_NAMES) to the properties of the infoblock elements ($TO_PROPERTY_NAMES).

The fields Date (DATE_ACTIVE_FROM) and Activity End (DATE_ACTIVE_TO) will be copied to the properties DATE_BEGIN and DATE_END of infoblock elements with ID = 22:

 function copy_from_fields_to_propertys_values( $IBLOCK_ID, $_FROM_FIELD_NAMES, $TO_PROPERTY_NAMES ){
    /* *
     * $_FROM_FIELD_NAMES = array(DATE_ACTIVE_FROM, DATE_ACTIVE_TO);
     * $TO_PROPERTY_NAMES = array(DATE_BEGIN, DATE_END);
     * copy_from_fields_to_propertys_values(22, array("DATE_ACTIVE_FROM","DATE_ACTIVE_TO"), array("DATE_BEGIN","DATE_END"));
     * */
        if ( CModule::IncludeModule ( "iblock" ) ){
            $arOrder = array(
                "sort" => "ASC",
            );

            $arFilter = array(
                "IBLOCK_ID" => $IBLOCK_ID,
            );

            foreach ( $TO_PROPERTY_NAMES as $property_name ) {
                $TO_PROPERTY_NAMES_with_prop[] = 'PROPERTY_' . $property_name;
            }

            $arSelect = array(
                "NAME",
                "ID"
            );

            $arSelect = array_merge ( $arSelect, $_FROM_FIELD_NAMES, $TO_PROPERTY_NAMES_with_prop );

            $db_elemens = CIBlockElement::GetList ( $arOrder, $arFilter, false, false, $arSelect );

            while ( $arElement = $db_elemens->Fetch () ) {
                $PRODUCT_ID = $arElement["ID"];

                foreach ( $TO_PROPERTY_NAMES as $key => $property_name ) {
                    CIBlockElement::SetPropertyValues ( $PRODUCT_ID, $IBLOCK_ID, $arElement[$_FROM_FIELD_NAMES[$key]], $property_name );
                }
            }

        } else {
            die( "The iblock module is not set" );
        }
    }

Additional Information





Courses developed by Bitrix24