//Установка свойств товара
foreach ($props[$id] as $code=>$values)
{
$properties = CIBlockProperty::GetList (
Array("sort"=>"asc", "name"=>"asc"),
Array("ACTIVE"=>"Y", "IBLOCK_ID"=>$IBLOCK_ID, 'CODE'=>$code)
);
//Добавляем свойства к инфоблоку, или заменяем старые
if ($prop_fields = $properties->Fetch() && empty ($param[$code]["$IBLOCK_ID.id"]))
{
//CIBlockProperty::Delete ($prop_fields['ID']); // -- не работает
//$query = "DELETE FROM b_iblock_property p, b_iblock_property_enum e, b_iblock_element_property l WHERE p.IBLOCK_ID=$IBLOCK_ID and p.CODE='$code' and e.PROPERTY_ID=p.ID and l.IBLOCK_PROPERTY_ID=p.ID"; // -- не работает
$query = "SELECT ID, IBLOCK_ID, CODE FROM b_iblock_property WHERE IBLOCK_ID=$IBLOCK_ID and CODE LIKE '$code'";
$r = mysql_query($query) or die (QueryError());
while ($row = mysql_fetch_assoc ($r))
{
//echo "b_iblock_property.ID = $row[ID], .CODE=$row[CODE] <br>\n";
mysql_query("DELETE FROM b_iblock_property_enum WHERE PROPERTY_ID=$row[ID]") or die (QueryError());
mysql_query("DELETE FROM b_iblock_element_property WHERE IBLOCK_PROPERTY_ID=$row[ID]") or die (QueryError());
mysql_query("DELETE FROM b_iblock_property WHERE ID=$row[ID]") or die (QueryError());
}
$new_prop_fields = Array(
"NAME" => $param[$code]['name'],
"ACTIVE" => "Y",
"SORT" => "500",
"CODE" => $code,
"PROPERTY_TYPE" => "L",
"IBLOCK_ID" => $IBLOCK_ID,
'MULTIPLE' => 'Y',
);
$i=0;
foreach ($param[$code]['values'] as $value)
$new_prop_fields["VALUES"][] = Array(
"VALUE" => $value,
"DEF" => "N",
"SORT" => (++$i)*100
);
$ibp = new CIBlockProperty;
if (!$param[$code]["$IBLOCK_ID.id"] = $ibp->Add ($new_prop_fields))
die ("Ошибка добавления свойства $code ".a2u($ibp->LAST_ERROR)."<br>\n");
$param[$code]['enum_id'] = Array ();
$property_enums = CIBlockPropertyEnum::GetList (
Array("SORT"=>"ASC"),
Array("IBLOCK_ID"=>$IBLOCK_ID, "CODE"=>$code)
);
while($enum_fields = $property_enums->GetNext())
$param[$code]['enum_id'][$enum_fields["VALUE"]] = $enum_fields["ID"];
}
//echo "<XMP>"; var_dump ($param); echo "</XMP>"; //exit;
//Добавляем свойства элемента из множественного списка напрямую в базу данных
foreach ($values as $value)
{
$value_id = $param[$code]['enum_id'][$value];
$query = "INSERT INTO b_iblock_element_property (IBLOCK_PROPERTY_ID, IBLOCK_ELEMENT_ID, VALUE, VALUE_TYPE, VALUE_ENUM)
VALUES (".$param[$code]["$IBLOCK_ID.id"].", $PRODUCT_ID, $value_id, 'text', $value_id)";
//echo a2u("<b>[$code, $value]</b>") . " $query <br>\n";
$r = mysql_query($query) or die (QueryError());
}
}
|