Skip to content

Instantly share code, notes, and snippets.

@VSeryoga
Created March 29, 2018 17:06
Show Gist options
  • Save VSeryoga/66b5f8b5d0bf151887d0ca1ef47e5b4b to your computer and use it in GitHub Desktop.
Save VSeryoga/66b5f8b5d0bf151887d0ca1ef47e5b4b to your computer and use it in GitHub Desktop.
Создание Торговых предложений при импорте Битркис
<?
function OnBeforeProductAdd($ID, $arFields){
$arFields['ID'] = $ID;
if($_SESSION['NO_ADD_TP']){
unset($_SESSION['NO_ADD_TP']);
return true;
}
CModule::IncludeModule('iblock');
CModule::includeModule("catalog");
$el = new CIBlockElement;
$arSelect = Array("ID", "NAME", "IBLOCK_ID", "DETAIL_PICTURE", "PROPERTY_*", "CATALOG_QUANTITY");
$arFilter = Array("IBLOCK_ID"=>6, "ID" => $arFields['ID']);
$res = CIBlockElement::GetList(Array('NAME' => 'DESC'), $arFilter, false, Array("nPageSize" => 1), $arSelect);
while($ob = $res->GetNextElement())
{
$arFields1 = $ob->GetFields();
// print_r($arFields);
$arProps = $ob->GetProperties();
// print_r($arProps);
$arrEl = $arFields1;
$arrEl['PROP']['ARTICULE']['VALUE'] = $arProps['ARTICULE']['VALUE'];
$arrEl['PROP']['AMMOUNT']['VALUE'] = $arProps['AMMOUNT']['VALUE'];
$arrEl['PROP']['BARCODE']['VALUE'] = $arProps['BARCODE']['VALUE'];
$arrEl['PROP']['MORE_PHOTO']['VALUE'] = $arProps['MORE_PHOTO']['VALUE'];
$arrEl['PROP']['QUANTITY']['VALUE'] = $arProps['QUANTITY']['VALUE'];
$name = preg_replace('/\s\d+\.?\d*\sмл/i', ' ###', $arFields1['NAME']);
$name = str_replace('###', $arProps['POL']['VALUE'], $name);
$name = str_replace(' тестер', '', $name);
$name = str_replace(' семпл', '', $name);
preg_match('/\d* гр/', $name, $matches);
if($matches[0]){
$arrEl['MASSA'] = $matches[0];
$name = str_replace(' '.$matches[0], '', $name);
}
preg_match('/Тон .*/', $name, $matches);
$ton = $matches[0];
$arrEl['TON'] = $matches[0];
if($matches[0]){
$name = str_replace(' '.$ton, '', $name);
}
$elU = new CIBlockElement;
$arLoadProductArrayU = array();
if(!$_SESSION['parentId'][$name]){
Debug::writeToFile('Поиск по названию');
$arSelect2 = Array("ID", "NAME", "IBLOCK_ID");
$arFilter2 = Array("IBLOCK_ID"=>6, "NAME" => $name.'%');
$res2 = CIBlockElement::GetList(Array('ID' => 'ASC'), $arFilter2, false, Array("nPageSize" => 1), $arSelect2);
while($ob2 = $res2->GetNextElement())
{
$arFields2 = $ob2->GetFields();
$_SESSION['parentId'][$name] = $arFields2['ID'];
$arLoadProductArrayU = Array(
"NAME" => $name
);
$resU = $elU->Update($arFields['ID'], $arLoadProductArrayU);
}
}
if($_SESSION['parentId'][$name] != $arFields['ID']){
$arLoadProductArrayU = Array(
"ACTIVE" => "N"
);
$resU = $elU->Update($arFields['ID'], $arLoadProductArrayU);
// $DB->StartTransaction();
// if(!CIBlockElement::Delete($arFields['ID']))
// {
// $strWarning .= 'Error!';
// Debug::writeToFile('Error!');
// $DB->Rollback();
// }
// else
// $DB->Commit();
}
$arrEl['NAME_REPLACE'] = $name;
$arrEl['NAME'] = $arFields1['NAME'];
$amount = false;
$PROP = array();
$PROP['ARTICULE'] = $arrEl['PROP']['ARTICULE']['VALUE'];
$amount = $arrEl['PROP']['AMMOUNT']['VALUE'];
if(substr_count($arrEl['NAME'], 'тестер')){
$amount = $arrEl['PROP']['AMMOUNT']['VALUE']." тестер";
}
if(substr_count($arrEl['NAME'], 'семпл')){
$amount = $arrEl['PROP']['AMMOUNT']['VALUE']." семпл";
}
if($arrEl['TON']){
$PROP['TON'] = getIdTon($arrEl['TON']);
}
if($amount){
$PROP['VALUE_FLACON'] = getIdAmount($amount);
}
$PROP['BARCODE'] = $arrEl['PROP']['BARCODE']['VALUE'];
$PROP['CML2_LINK'] = $_SESSION['parentId'][$name];
if($value['PROP']['MORE_PHOTO']['VALUE']){
foreach ($arrEl['PROP']['MORE_PHOTO']['VALUE'] as $val) {
$PROP['MORE_PHOTO'][] = CFile::MakeFileArray($val);
}
}
$arLoadProductArray = Array(
"IBLOCK_SECTION_ID" => false,
"IBLOCK_ID" => 7,
"PROPERTY_VALUES"=> $PROP,
"NAME" => $arrEl['NAME'],
"ACTIVE" => "Y", // активен
// "PREVIEW_TEXT" => $arItem['UF_DATA']['desc'],
"DETAIL_PICTURE" => CFile::MakeFileArray($arrEl['DETAIL_PICTURE']),
"PREVIEW_PICTURE" => CFile::MakeFileArray($arrEl['DETAIL_PICTURE'])
);
$_SESSION['NO_ADD_TP'] = true;
if($PRODUCT_ID = $el->Add($arLoadProductArray, false, false, true)){
$arFieldsP = array(
"ID" => $PRODUCT_ID,
'TYPE' => 4,
"QUANTITY" => $arrEl['CATALOG_QUANTITY']
);
$productID = CCatalogProduct::Add($arFieldsP);
}else{
echo "Error: ".$el->LAST_ERROR;
}
}
// $arFields['TYPE'] = 3;
return true;
}
}
function getIdAmount($value){
CModule::IncludeModule('iblock');
if($_SESSION['propAmount'][$value]){
return $_SESSION['propAmount'][$value];
}else{
$ibpenum = new CIBlockPropertyEnum;
$property_enums = CIBlockPropertyEnum::GetList(Array(), Array("IBLOCK_ID"=>7,
"CODE"=>"VALUE_FLACON",
"VALUE" => $value)
);
if($enum_fields = $property_enums->GetNext()){
$_SESSION['propAmount'][$enum_fields["VALUE"]] = $enum_fields["ID"];
return $enum_fields["ID"];
}else{
if($PropID = $ibpenum->Add(Array('PROPERTY_ID'=>116, 'VALUE'=>$value))){
$_SESSION['propAmount'][$value] = $PropID;
return $PropID;
}
}
}
}
function getIdTon($value){
CModule::IncludeModule('iblock');
if($_SESSION['propTon'][$value]){
return $_SESSION['propTon'][$value];
}else{
$ibpenum = new CIBlockPropertyEnum;
$property_enums = CIBlockPropertyEnum::GetList(Array(), Array("IBLOCK_ID"=>7,
"CODE"=>"TON",
"VALUE" => $value)
);
if($enum_fields = $property_enums->GetNext()){
$_SESSION['propTon'][$enum_fields["VALUE"]] = $enum_fields["ID"];
return $enum_fields["ID"];
}else{
if($PropID = $ibpenum->Add(Array('PROPERTY_ID'=>144, 'VALUE'=>$value))){
$_SESSION['propTon'][$value] = $PropID;
return $PropID;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment