Last active
April 19, 2024 09:04
-
-
Save EscApp2/86c7ffdce5ffa7d0c4eac26a38e2ff44 to your computer and use it in GitHub Desktop.
bitrix get last viewed products
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sku_id=>element_id | |
function GetLastViewed($limit = false){ | |
// \bitrix\components\bitrix\catalog.viewed.products\class.php #657 | |
$map = array(); | |
if (!Bitrix\Main\Loader::includeModule('catalog')){ | |
return array(); | |
} | |
if (!Bitrix\Main\Loader::includeModule('sale')){ | |
return array(); | |
} | |
$basketUserId = (int)\Bitrix\Sale\Fuser::getId(false); | |
//$basketUserId = (int)CSaleBasket::GetBasketUserID(false); | |
if ($basketUserId <= 0){ | |
return array(); | |
} | |
$filter = array('=FUSER_ID' => $basketUserId, '=SITE_ID' => SITE_ID); | |
$ar_all_filter = array( | |
'select' => array('PRODUCT_ID', 'ELEMENT_ID'), | |
'filter' => $filter, | |
'order' => array('DATE_VISIT' => 'DESC'), | |
); | |
if(!empty($limit)){ | |
$ar_all_filter['limit'] = $limit; | |
} | |
$viewedIterator = Bitrix\Catalog\CatalogViewedProductTable::getList($ar_all_filter); | |
unset($filter); | |
while ($viewedProduct = $viewedIterator->fetch()) | |
{ | |
$viewedProduct['ELEMENT_ID'] = (int)$viewedProduct['ELEMENT_ID']; | |
$viewedProduct['PRODUCT_ID'] = (int)$viewedProduct['PRODUCT_ID']; | |
$map[$viewedProduct['PRODUCT_ID']] = $viewedProduct['ELEMENT_ID']; | |
if ($viewedProduct['ELEMENT_ID'] <= 0) | |
$emptyProducts[] = $viewedProduct['PRODUCT_ID']; | |
} | |
if (!empty($emptyProducts)) | |
{ | |
$emptyProducts = Bitrix\Catalog\CatalogViewedProductTable::getProductsMap($emptyProducts); | |
if (!empty($emptyProducts)) | |
{ | |
foreach ($emptyProducts as $product => $parent) | |
{ | |
$map[$product] = $parent; | |
} | |
} | |
} | |
return $map; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment