You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
aliseptik/local/ajax/search.php

53 lines
2.1 KiB

<?php
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
header('Content-Type: application/json; charset=UTF-8');
use Bitrix\Main\Loader;
use Bitrix\Iblock\ElementTable;
$query = isset($_GET['q']) ? trim($_GET['q']) : '';
$results = [];
if ($query && Loader::includeModule('iblock') && Loader::includeModule('catalog')) {
$iblockId = 4; // <-- Укажите ID вашего инфоблока с товарами
$res = CIBlockElement::GetList([
'SORT' => 'ASC'
], [
'IBLOCK_ID' => $iblockId,
'ACTIVE' => 'Y',
'%NAME' => $query
], false, ['nTopCount' => 10], ['ID', 'NAME', 'DETAIL_PAGE_URL', 'PREVIEW_PICTURE', 'DETAIL_PICTURE']);
while ($item = $res->GetNext()) {
$img = '';
if ($item['PREVIEW_PICTURE']) {
$img = CFile::GetPath($item['PREVIEW_PICTURE']);
} elseif ($item['DETAIL_PICTURE']) {
$img = CFile::GetPath($item['DETAIL_PICTURE']);
}
// Абсолютный путь к картинке
if ($img && strpos($img, 'http') !== 0) {
global $APPLICATION;
$protocol = (method_exists($APPLICATION, 'IsHTTPS') && $APPLICATION->IsHTTPS()) ? 'https' : 'http';
$img = $protocol . '://' . $_SERVER['SERVER_NAME'] . $img;
}
$url = $item['DETAIL_PAGE_URL'];
if ($url && $url[0] !== '/') {
$url = '/' . ltrim($url, '/');
}
// Получаем цену
$price = '';
if (Loader::includeModule('catalog')) {
$arPrice = CCatalogProduct::GetOptimalPrice($item['ID'], 1, array(), 'N');
if ($arPrice && isset($arPrice['RESULT_PRICE']['DISCOUNT_PRICE'])) {
$price = number_format($arPrice['RESULT_PRICE']['DISCOUNT_PRICE'], 0, '', ' ') . ' ₽';
}
}
$results[] = [
'title' => $item['NAME'],
'url' => $url,
'img' => $img,
'price' => $price
];
}
}
echo json_encode($results, JSON_UNESCAPED_UNICODE);