📜 ⬆️ ⬇️

Deferred functions in 1C-Bitrix

Again, a note on the topic of 1C-Bitrix, so please: Habrelochek people who have already pulled their mouse to put a minus, just close the page and do not prevent the rest to get useful (I hope) information.

I have been engaged in the development of sites based on Bitrix for a long time, and during this time several useful tips have accumulated, one of which I will now share.

Deferred Functions
Many times I came across a mention in the documentation of deferred functions, it was clear that this is something interesting, but as you know it is better to try once than to read about it seven times . What is it? Here is the definition that can be found on the official documentation page on this technology:
Deferred functions is a technology that allows you to set the page title, navigation chain items, CSS styles, additional buttons in the control panel, meta tags, etc. using the functions used directly in the page body, and output the corresponding results of the work of these functions in the prolog, i.e. output them higher in code than they were given.
I found these functions useful when creating an online store, namely to work with a basket. In the store header, in the most visible place hung a basket, it displayed the purchase amount and the quantity of goods. But when you add the product you like to the cart, this inscription has not changed, because the header is formed and displayed earlier than the component is processing the addition of the product to the cart, and then updating its contents. Then I began to look towards LocalRedirect to reload the page after successfully completing the add operation. But those deferred functions helped out. And you can do this here.
In the header, where our cart is displayed we write the following code:
<? $APPLICATION->ShowProperty("basket", " "); ?>

Add the following lines to footer:
if (CModule::IncludeModule("sale")){
$arItems = GetBasketList();
$price = 0;
for ($i = 0; $i<count($arItems); $i++){
$num_prodcts += $arItems[$i]["QUANTITY"];
$price += $arItems[$i]["PRICE"]*$arItems[$i]["QUANTITY"];
}
$res = $num_prodcts.' (.): '.number_format($price, 0, ".", " ").' .
}
else{
$res = " ";
}
$APPLICATION->SetPageProperty("basket", $res);
}

Now, after processing the addition of goods in the body of the page, we check the status of the current basket and output it via the deferred function ShowProperty to the header .

')

Source: https://habr.com/ru/post/40214/


All Articles