S
S
symply_rabbit2019-04-08 20:42:27
1C-Bitrix
symply_rabbit, 2019-04-08 20:42:27

How to write a value to the Bitrix property for all products?

Good evening! The problem is that you need to add a value to the property for all products. but is added to only one product if we $arFilterwrite "ID"=>"707"then the value is written to the property. but when I remove this inscription "ID"=>"707"and the code for writing the value to the properties was executed for all products, it does not work. code below. what's my mistake?

<?
Cmodule::IncludeModule('catalog');
CModule::IncludeModule("iblock"); 
?>




<?
$arSelect = Array("ID", "IBLOCK_ID", "NAME", "DATE_ACTIVE_FROM","PROPERTY_*");
$arFilter = Array("IBLOCK_ID"=>18, "ACTIVE_DATE"=>"Y", "ACTIVE"=>"Y");
$res = CIBlockElement::GetList(Array(), $arFilter, false, Array("nPageSize"=>50), $arSelect);
while($ob = $res->GetNextElement()){ 

 $arFields = $ob->GetFields();  
//print_r($arFields);

echo $arFields[ID];?><br><?
 $arProps = $ob->GetProperties();


//print_r($arProps);
//echo $arProps[TSENA_ZA_KG][VALUE];

// предполагаем, что валюты RUB и EUR существуют в базе
$val = $arProps[TSENA_ZA_KG][VALUE]; // сумма в EUR
$newval = round(CCurrencyRates::ConvertCurrency($val, "EUR", "RUB"), 2);
echo $newval;


// добавление в свойство "по цене"

CIBlockElement::SetPropertyValuesEx($arFields[ID], false, array("PRICE_FILTER" => $newval));


$arFields = array("PRICE_FILTER" => $newval);// зарезервированное количество
CCatalogProduct::Update($arFields[ID], $arFields);


}
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Proger, 2019-04-08
@freekirill

"nPageSize"=>50 - costs 50 products to display. those. actions will be performed only for the first 50 products.

R
Roman Gritsuk, 2019-04-08
@winer

1) Don't forget that array keys are strings. $arProps[TSENA_ZA_KG][VALUE] -> $arProps["TSENA_ZA_KG"]["VALUE"
]

$arFields = array("PRICE_FILTER" => $newval);// зарезервированное количество
CCatalogProduct::Update($arFields["ID"], $arFields);

CCatalogProduct::Update - This is a method from the Catalog module that updates the product parameters (b_catalog_product), not the infoblock element.
You are passing a non-existent "PRICE_FILTER" field to this method. Most likely you have disabled PHP error output and when an error occurs, you stop further script execution without displaying any information on the screen.
3) As already mentioned, the "nPageSize"=>50 parameter is set in CIblockElement::GetList. Because of it, only 50 items will be selected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question