📜 ⬆️ ⬇️

Sorting the goods and displaying the quantity of goods selected by the user in 1C-Bitrix

Historically, the integrated component 1C-Bitrix does not allow the user to sort products in the public part, at least by price, date, name, and also select how many products on the page to select. But none of the online stores can do without such functionality, which by the way is included in almost all templates of ready-made online stores in the Marketplace. But to implement the blocks “Sort by: ...” and “Show by: ...” is quite simple. All you need is to use the $_REQUEST and the API API 1C-Bitrix GetCurPageParam() to transfer data to this array.

Let's get started!

To begin with, we will define what they want from us:


Let's go in order and start by showing the user selected from the proposed number of products on the page.
')
For displaying the number of products on the page corresponds to the parameter PAGE_ELEMENT_COUNT . In it, we will pass the amount selected by the user using $_REQUEST and GetCurPageParam() .

To begin, let us create the corresponding variable and, by default, assign the value specified in the component parameters to it. And then when the user clicks on the number of elements he needs, we will modify it.

The following code is proposed to be placed before connecting the component catalog.section . If the complex catalog component is used, then in the file in which the component catalog.section component catalog.section goods on the necessary page.

 <?php $pageElementCount = $arParams["PAGE_ELEMENT_COUNT"]; if (array_key_exists("showBy", $_REQUEST)) { if ( intVal($_REQUEST["showBy"]) && in_array(intVal($_REQUEST["showBy"]), array(18, 36, 54, 72)) ) { $pageElementCount = intVal($_REQUEST["showBy"]); $_SESSION["showBy"] = $pageElementCount; } elseif ($_SESSION["showBy"]) { $pageElementCount = intVal($_SESSION["showBy"]); } } ?> <div class="show_number"> <span class="show_title">  </span> <span class="number_list"> <? for( $i = 18; $i <= 72; $i+=18 ) : ?> <a rel="nofollow" <? if ($i == $pageElementCount): ?>class="current"<? endif; ?> href="<?= $APPLICATION->GetCurPageParam('showBy='.$i, array('showBy', 'mode')) ?>" > <span><?= $i ?></span> </a> <? endfor; ?> </span> </div> 

And now we transfer the obtained value to the component:

 <?php $APPLICATION->IncludeComponent( "bitrix:catalog.section", ... "PAGE_ELEMENT_COUNT" => $pageElementCount, ... ?> 

Now let's deal with sorting. It is not much more difficult, except for the fact that we need to check the current direction and change it. To do this, in the GetCurPageParam() method we will pass two parameters sortBy and orderBy . And then in the corresponding variables pass them to the parameters of the component "ELEMENT_SORT_FIELD" and "ELEMENT_SORT_ORDER" respectively. By default, sorting should be carried out using the internal sorting field 1C-Bitrix - sort .

Check the direction of sorting and change if necessary:

 <?php if (isset($_REQUEST['orderBy'])) { if ($_REQUEST['orderBy'] == 'asc') { $orderBy = 'desc'; } else { $orderBy = 'asc'; } } else { $orderBy = 'asc'; } ?> 

We display links to sorting:

 <div class="sort-section">  : <a rel="nofollow" <? if ($sortBy == 'price') : ?> class="current-sort" <? endif; ?> href="<?= $APPLICATION->GetCurPageParam('sortBy=price&orderBy='.$orderBy, array('sortBy', 'orderBy')) ?>" > <span class="sort"></span> </a> <a rel="nofollow" <? if ($sortBy == 'name') : ?> class="current-sort" <? endif; ?> href="<?= $APPLICATION->GetCurPageParam('sortBy=name&orderBy='.$orderBy, array('sortBy', 'orderBy')) ?>" > <span class="sort"></span> </a> <a rel="nofollow" <? if ($sortBy == 'date') : ?> class="current-sort" <? endif; ?> href="<?= $APPLICATION->GetCurPageParam('sortBy=date&orderBy='.$orderBy, array('sortBy', 'orderBy')) ?>" > <span class="sort"></span> </a> </div> 

The name parameter is accepted by the "ELEMENT_SORT_FIELD" field for sorting by product name without additional intervention, but for the date and price we need to clarify which of the information block element parameters we mean.

For the date, we need the value of the 'timestamp_x' field, which is responsible for the change date. For the price, we need to find out the name of the price type, which is derived from the information block element field. To do this, print the array $arItem in the component template (in my case catalog.section ) or with var_dump($arItem); either
 echo ''; print_r($arItem); echo ' '; 
echo ''; print_r($arItem); echo ' '; . We find the array field responsible for the price output and copy its name, in my case it turned out to be CATALOG_PRICE_1 . It should be noted that in the case of the price you need to use the name of the field that contains the value of the price without a currency.

And pass the data to the $sortBy variable:

 <?php if (isset($_REQUEST['sortBy'])) { $sortBy = $_REQUEST['sortBy']; } else { $sortBy = 'sort'; } if ($sortBy=='price') { $sortBy = 'CATALOG_PRICE_1'; } if ($sortBy=='date') { $sortBy = 'timestamp_x'; } ?> 

And pass the values ​​to the component parameters:

 <?php $APPLICATION->IncludeComponent( "bitrix:catalog.section", ... "ELEMENT_SORT_FIELD" => $sortBy, "ELEMENT_SORT_ORDER" => $orderBy, ... ?> 

That's all. The result is something like this:

image

Now the user can sort the goods and choose how many goods he wants to see on the page, and of course, go through the pages and filter the goods without losing anything.

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


All Articles