mr.rifa
7/20/2018 - 6:54 AM

Импорт файлов в товары

Импортируем каталог файлов расположенный на сервере в инфоблок товаров

<?php
$time_start = microtime(true);

$_SERVER["DOCUMENT_ROOT"] = realpath(dirname(__FILE__)."/.." ) ;
$DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"];
define("NO_KEEP_STATISTIC", true);
define("NOT_CHECK_PERMISSIONS",true);
define('CHK_EVENT', true);
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php');

$dir    = realpath(__DIR__."/img/") ;
echo $dir." ";
$files = scandir($dir);
$allFiles = [];
CModule::IncludeModule('iblock');
foreach($files as $file){
echo $file." ";
    if( ($file == ".") || ($file == "..") )
        continue;
    
    $arName = explode("#", $file);
    
    $artNumber = '';
    if(count($arName) != 1){
        $artNumber = trim($arName[0]);
        $arFile = [];
        $arFile = CFile::MakeFileArray($_SERVER["DOCUMENT_ROOT"]."/cronjobs/img/".$file);
        $arFile["MODULE_ID"] = "iblock";
        $allFiles[$artNumber][] = $arFile;  
        
        
    }
}

if(!empty($allFiles)){
    foreach($allFiles as $art_n=>$fs){
        $arSelect = Array("ID", "PROPERTY_ARTNUMBER", "DETAIL_PICTURE");
        $arFilter = Array("IBLOCK_ID"=>IB_PRODUCTS, "ACTIVE_DATE"=>"Y", "ACTIVE"=>"Y", "PROPERTY_ARTNUMBER" => $art_n);
        $resItems = CIBlockElement::GetList(Array(), $arFilter, false, false, $arSelect);
        while($obItems = $resItems->GetNext()){
            foreach($fs as $f)
                CIBlockElement::SetPropertyValueCode($obItems["ID"], "MORE_PHOTO", Array("VALUE"=>$f)  );
            
            if(empty($obItems["DETAIL_PICTURE"])){
                $el = new CIBlockElement;
                $arLoadProductArray = Array(
                    "DETAIL_PICTURE" => $fs[0]
                );

                $res = $el->Update($obItems["ID"], $arLoadProductArray);
            }
        }
    }
}

print_r($allFiles);