E
E
Ex1st2021-08-10 13:17:52
1C-Bitrix
Ex1st, 2021-08-10 13:17:52

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:

reveal the code
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);
}

The event fires, but the price is not added.

Googled, it turns out there is a similar bug from 2012. I did not try the solution from there, because I did not understand how to implement it.

Question: what's wrong? And is the bug relevant, who faced it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tgarl, 2021-08-10
@tgarl

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 question

Ask a Question

731 491 924 answers to any question