I
I
I'm Yoda2018-11-29 05:54:41
1C-Bitrix
I'm Yoda, 2018-11-29 05:54:41

How to get the number of available products in sections?

Hello connoisseurs! Customizing menu.sections, I want to display the number of available products in the sections in the menu. How to recursively get the number of available items?

while ($arSection = $rsSections->GetNext()) {

            $resElemCnt = CIBlockElement::GetList(
                            false, array(
                        "IBLOCK_ID" => $arParams["IBLOCK_ID"],
                        "IBLOCK_SECTION_ID" => $arSection["ID"],
                        "ACTIVE" => "Y",
                        "INCLUDE_SUBSECTIONS" => "Y",
                        "SUBSECTION" => "Y",
                        "CATALOG_AVAILABLE " => "Y",
                            ), false, false, array("ID"));

            $arResult["SECTIONS"][] = array(
                "ID" => $arSection["ID"],
                "DEPTH_LEVEL" => $arSection["DEPTH_LEVEL"],
                "CNT" => $arSection["ELEMENT_CNT"], // Показывает все активные элементы, надо только доступные
                //"CNT" => $resElemCnt->SelectedRowsCount(), // Показывает только доступные товары! Считает почти верно, но родительский раздел показывает 0.
                "~NAME" => $arSection["~NAME"],
                "~SECTION_PAGE_URL" => $arSection["~SECTION_PAGE_URL"],
            );
            $arResult["ELEMENT_LINKS"][$arSection["ID"]] = array();
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Gritsuk, 2018-11-29
@Anadi

Displaying the number of products in sections, with a large catalog - one of the ways to make the site slow.
In order to count the number of products in sections, you have to do SELECT N + 1 (requests in a loop).
In your case, it's even worse, because. to filter products by availability, use the CATALOG_AVAILABLE filter, which in turn will additionally include the b_catalog_product table in the SQL query. The end result is a slow site.
If it is not possible to refuse the output of the quantity, then you can do this:
Thus, you will transfer the load of calculating the number of goods from hits to agents, and the process of forming a menu block will not take much time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question