G
G
Georgy Baruchyan2019-04-14 17:32:22
1C-Bitrix
Georgy Baruchyan, 2019-04-14 17:32:22

How to describe price change events (in fresh Bitrix)?

Hello!
The following need arose: when changing the price of an element, write the price value to the property of this element of the infoblock.
Bitrix version 18.0.9
Previously, it was possible to use the OnPriceUpdate(Add) or OnBeforePriceUpdate(Add)
event. However, when describing event data handlers, nothing happens. In the handlers, I just dumped the fields into a file to make sure they were working.
Crawling through the documentation, I found that the CPrice::Update and Add methods are declared as deprecated and it is recommended to use \Bitrix\Catalog\Model\Price::update
In the old kernel, CPrice::Update triggered events, in the new one it calls the previously specified \Bitrix \Catalog\Model\Price::update which in turn does NOT trigger events.
Hence the question arises, how to track the change / addition of the price?
I tried to bypass, through infoblock events, after saving the element (OnAfterIBlockElementUpdate), get the price from the price table (\Bitrix\Catalog\PriceTable) and write it to the properties, but the problem is that all events for the element are executed first, and then the price is written , and when saving, we can only get the old price!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Gritsuk, 2019-04-14
@Snatch87

1st option:
go to Settings > Product settings > Modules settings > Trade catalog and enable "Enable legacy events support" there
2nd option:
Rewrite event to D7 . Attach a handler to the \Bitrix\Catalog\Price::OnAfterUpdate event:

$bxEventManager->addEventHandler(
    'catalog',
    '\Bitrix\Catalog\Price::OnAfterUpdate',
    [
        \Yout\Namesspace\ClassName::class,
        "onAfterUpdatePriceHandler"
    ]
);

And in the handler \Yout\Namesspace\ClassName::onAfterUpdatePriceHandler already describe your logic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question