Answer the question
In order to leave comments, you need to log in
How to change the price when adding/changing an element of the trade catalog?
You need to generate a price based on the purchase price.
To do this, I decided to use the "OnProductAdd" event and this is what happened:
function ProductAddPriceHandler($id, $arFields)
{
$purchasePrice = $arFields['PURCHASING_PRICE'];
$id = $arFields['ID'];
if ($purchasePrice <= 100) {
$productPrice = ($purchasePrice / 100 * 100) + $purchasePrice;
} elseif ($purchasePrice >= 101 and $purchasePrice <= 500) {
$productPrice = ($purchasePrice / 100 * 75) + $purchasePrice;
} elseif ($purchasePrice >= 501 and $purchasePrice <= 1000) {
$productPrice = ($purchasePrice / 100 * 60) + $purchasePrice;
} elseif ($purchasePrice >= 1001 and $purchasePrice <= 2000) {
$productPrice = ($purchasePrice / 100 * 50) + $purchasePrice;
} elseif ($purchasePrice >= 2001 and $purchasePrice <= 4000) {
$productPrice = ($purchasePrice / 100 * 40) + $purchasePrice;
} elseif ($purchasePrice >= 4001) {
$productPrice = ($purchasePrice / 100 * 30) + $purchasePrice;
}
$arProductPriceFields = array(
'PRODUCT_ID' => $id,
'CATALOG_GROUP_ID' => 1,
'PRICE' => 500,
'CURRENCY' => 'RUB',
);
$result = \Bitrix\Catalog\Model\Price::add($arProductPriceFields);
}
Answer the question
In order to leave comments, you need to log in
Your main mistake is that you just want to add a price, but the product may already have it. Therefore, you first need to get the item's price id. If not, then add, otherwise update.
If you look in the documentation, then there is a good example
$res = CPrice::GetList(
array(),
array(
"PRODUCT_ID" => $PRODUCT_ID,
"CATALOG_GROUP_ID" => $PRICE_TYPE_ID
)
);
if ($arr = $res->Fetch())
{
CPrice::Update($arr["ID"], $arFields); //если есть обновляем
}
else
{ //иначе добавляем
CPrice::Add($arFields);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question